var arr = [
{name: 'A', type: 'fly'},
{name: 'B', type: 'blur'},
{name: 'C', type: 'wipe'},
{name: 'Y', type: 'bar'},
{name: 'D', type: 'cube'},
{name: 'B', type: 'zoom'},
{name: 'C', type: 'cube'},
{name: 'E', type: 'iris'},
{name: 'F', type: 'fade'},
{name: 'O', type: 'foo'},
{name: 'G', type: ''}
];
arr.sort((star,next)=>{
// 按type的指定顺序排序
var order = ["wipe", "fly", "iris", "flip", "cube",
"blur", "fade",'zoom','bar','foo',''];
return order.indexOf(star.type) - order.indexOf(next.type)
})
console.log(arr)
js使用sort()自定义规则排序
最新推荐文章于 2024-01-17 18:31:29 发布