首页 > 随笔档案 > 桶排序-Node.js-对象排序

桶排序-Node.js-对象排序

Publish:

const b = [{index:5,name:"s5"}, {index:2,name:"s2"}, {index:3,name:"s3"}, {index:1,name:"s1"}, {index:8,name:"s8"}];
var a = [],
    i;
 
for (var i = 0; i < b.length; i++) {
    var num = b[i].index;
    a[num] = a[num]||{};
    a[num] = b[i]; 
    num == null;
}
 
for (var i = 0; i < a.length; i++) {
    if(a[i]) console.log(a[i].name);
}

声明: 本文采用 BY-NC-SA 授权。转载请注明转自: levy