JavaScript array sort on 2 properties

后端 未结 2 1083
误落风尘
误落风尘 2021-01-29 11:18

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

var garments [{
    name: \'Garment 1\',
    isDesignable: false,
    priority: 3
},{
    name: \'         


        
2条回答
  •  猫巷女王i
    2021-01-29 12:04

    garments.sort(function (a, b) {
        if (a.isDesignable == b.isDesignable) {
            return a.priority - b.priority;
        } else if (a.isDesignable) {
            return -1;
        } else {
            return 1;
        }
    });
    

提交回复
热议问题