JavaScript数组排序2性能 [英] JavaScript array sort on 2 properties

查看:205
本文介绍了JavaScript数组排序2性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有对象的JSON数组,看起来是这样的:

  VAR服装[{
    名称:服装1',
    isDesignable:假的,
    优先级:3
},{
    名称:'服装2',
    isDesignable:假的,
    优先级:1
},{
    名称:'服装3'
    isDesignable:真实,
    优先级:3
},{
    名称:'服装4,
    isDesignable:真实,
    优先级:2
},{
    名称:'服装5',
    isDesignable:真实,
    优先级:4
}];

起初,我需要通过数组排序的优先级所以我这样做:

  garments.sort(功能(A,B){    //按优先级
    返回a.priority - b.priority;
});

这是罚款。但现在,我已经意识到,如果衣服不是设计性,那么它应该在阵列底部,无论它的优先级。谁能帮我排序功能,使所有非可设计的服装是在sort的底部?


解决方案

与code的一条线解决方案。

先建 isDesignable 之间的差异,如果同一应用的不同优先级作为排序指标。

\r
\r

VAR服装= {[\r
        名称:服装1',\r
        isDesignable:假的,\r
        优先级:3\r
    },{\r
        名称:'服装2',\r
        isDesignable:假的,\r
        优先级:1\r
    },{\r
        名称:'服装3'\r
        isDesignable:真实,\r
        优先级:3\r
    },{\r
        名称:'服装4,\r
        isDesignable:真实,\r
        优先级:2\r
    },{\r
        名称:'服装5',\r
        isDesignable:真实,\r
        优先级:4\r
    }];\r
//排序isDesignable先按照优先级升序\r
garments.sort(功能(A,B){\r
    返回b.isDesignable - a.isDesignable || a.priority - b.priority;\r
});\r
的document.write('< pre>'+ JSON.stringify(服装,0,4)+'< / pre>');\r
\r
//排序排序扭转前排序顺序,而不是逆转\r
garments.sort(功能(A,B){\r
    返回a.isDesignable - b.isDesignable || b.priority - a.priority;\r
});\r
的document.write('< pre>'+ JSON.stringify(服装,0,4)+'< / pre>');

\r

\r
\r

I have a JSON array of objects that looks something like this:

var garments [{
    name: 'Garment 1',
    isDesignable: false,
    priority: 3
},{
    name: 'Garment 2',
    isDesignable: false,
    priority: 1
},{
    name: 'Garment 3',
    isDesignable: true,
    priority: 3
},{
    name: 'Garment 4',
    isDesignable: true,
    priority: 2
},{
    name: 'Garment 5',
    isDesignable: true,
    priority: 4
}];

Initially, I needed to sort the array by priority so I did this:

garments.sort(function (a, b) {

    // By priority
    return a.priority - b.priority;
});

which was fine. But now, I have realised that if a garment is not designable, then it should be at the bottom of the array regardless of it's priority. Can anyone help me with the sort function so all non designable garments are at the bottom of the sort?

解决方案

Solution with one line of code.

First build the difference between isDesignable and if the same apply the difference of priority as sort indicator.

var garments = [{
        name: 'Garment 1',
        isDesignable: false,
        priority: 3
    }, {
        name: 'Garment 2',
        isDesignable: false,
        priority: 1
    }, {
        name: 'Garment 3',
        isDesignable: true,
        priority: 3
    }, {
        name: 'Garment 4',
        isDesignable: true,
        priority: 2
    }, {
        name: 'Garment 5',
        isDesignable: true,
        priority: 4
    }];
// sort isDesignable first and then by priority ascending
garments.sort(function (a, b) {
    return b.isDesignable - a.isDesignable || a.priority - b.priority;
});
document.write('<pre>' + JSON.stringify(garments, 0, 4) + '</pre>');

// sort reversing the former sort order by sorting, not reversing
garments.sort(function (a, b) {
    return a.isDesignable - b.isDesignable || b.priority - a.priority;
});
document.write('<pre>' + JSON.stringify(garments, 0, 4) + '</pre>');

这篇关于JavaScript数组排序2性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆