先定义一个需要自定义排序的数组(或者是乱序数组)
var objs = [
{‘name’: ‘A’, ‘type’: ‘fly’},
{‘name’: ‘B’, ‘type’: ‘blur’},
{‘name’: ‘C’, ‘type’: ‘wipe’},
{‘name’: ‘D’, ‘type’: ‘cube’},
{‘name’: ‘E’, ‘type’: ‘iris’},
{‘name’: ‘F’, ‘type’: ‘fade’}
];
在此数组里再次自定义排序
objs.sort(function(a,b){
// order是规则 objs是需要排序的数组
var order = [“wipe”, “fly”, “iris”, “flip”, “cube”,“blur”, “zoom”, “fade”, “glow”, “rotate”];
return order.indexOf(a.type) - order.indexOf(b.type);
});
js数组按照指定字来排序
最新推荐文章于 2024-05-24 16:55:11 发布