排序对象数组时,是否可以使用sort()进一步将已推到底部的项目分组?

时间:2017-03-22 09:34:47

标签: javascript arrays sorting

我在JavaScript中这样做,我必须在单个sort()函数中执行此操作,因为它是作为参数发送的。

例如,我有按类别排序的对象,然后我使用以下方式按子类别进行排序:

return item1.category.localeCompare(item2.category) : item1.subCategory.localeCompare(item2.subCategory)

但是对于没有类别的对象,我使用:

推到底部
    if(item1.category == ''){
        res = 1
    }
    if(item2.category == ''){
        res = -1
    }

这个问题是所有这些项目都在底部,但它们都混乱了,我需要的是按子类别将它们组合在一起。

我可以对它们进行排序的对象示例:

[
  {'category':'cat1', 'subcategory':'subcat1'},
  {'category':'cat1', 'subcategory':'subcat2'},
  {'category':'cat2', 'subcategory':'subcat1'},
  {'category':'cat2', 'subcategory':'subcat2'}
  {'category':'', 'subcategory':'subcat1'},
  {'category':'', 'subcategory':'subcat2'},
  {'category':'', 'subcategory':'subcat1'},
  {'category':'', 'subcategory':'subcat2'}
]

我需要成为:

[
  {'category':'cat1', 'subcategory':'subcat1'},
  {'category':'cat1', 'subcategory':'subcat2'},
  {'category':'cat2', 'subcategory':'subcat1'},
  {'category':'cat2', 'subcategory':'subcat2'}
  {'category':'', 'subcategory':'subcat1'},
  {'category':'', 'subcategory':'subcat1'},
  {'category':'', 'subcategory':'subcat2'},
  {'category':'', 'subcategory':'subcat2'}
]

3 个答案:

答案 0 :(得分:3)

您可以先将空白部分排序到底部,然后比较字符串。同时选择categorysubcategory



var data = [{ category: 'cat2', subcategory: 'subcat1' }, { category: 'cat1', subcategory: 'subcat1' }, { category: 'cat2', subcategory: 'subcat2' }, { category: '', subcategory: '' }, { category: '', subcategory: 'subcat1' }, { category: '', subcategory: 'subcat2' }, { category: 'cat1', subcategory: 'subcat2' }, { category: 'cat1', subcategory: '' }, { category: 'cat2', subcategory: '' }];

data.sort(function (a, b) {
    return (
        !a.category - !b.category ||                 // move falsy values to bottom
        a.category.localeCompare(b.category) ||      // sort strings
        !a.subcategory - !b.subcategory || 
        a.subcategory.localeCompare(b.subcategory) 
    );
});

console.log(data);

.as-console-wrapper { max-height: 100% !important; top: 0; }




答案 1 :(得分:0)

您甚至可以使用辅助函数动态执行此操作,并以嵌套方式对任意数量的属性进行排序。

function getFakeObjects(n){  // helper function to get test objects
  return Array(n).fill()
                 .reduce(o => o.concat({p1 : String.fromCharCode(...Array(5).fill().map(_ => ~~(Math.random()*26)+97)),
                                        p2 : ~~(Math.random()*5)+1,
                                        p3 : ["Istanbul","Moscow","New York","Tokyo","Rio","Accra"][~~(Math.random()*6)],
                                        p4 : ~~(Math.random()*2+1)+String.fromCharCode(~~(Math.random()*3)+65)
                                       }),[]);
}

function multiLevelSorter(os,ps){
  return os.sort(function(a,b){
                   var p = ps.find(f => a[f] !== b[f]);
                   return a[p] < b[p] ? -1 : 1;
                 });
}

var fakes = getFakeObjects(50),
sortOrder = ["p3","p2","p4","p1"],
   sorted = multiLevelSorter(fakes,sortOrder);
sorted.forEach(o => console.log("{p1:", o.p1,"p2:",o.p2,"p3:",o.p3,"p4:",o.p4,"}"));
.as-console-wrapper { max-height: 100% !important; top: 0; }

所以上面的代码在p3的优先级中排序 - > p2 - &gt; p4 - &gt; P1。

答案 2 :(得分:0)

您还可以使用SortedArrayCategory。代码如下:

// arrTemp =您的实际数组名称。

                                self.sortedArrayCategory = arrTemp.sortedArrayUsingComparator {
                                    (obj1, obj2) -> NSComparisonResult in

                                    let p1 = obj1 as! String
                                    let p2 = obj2 as! String
                                    let result = p1.compare(p2)
                                    return result
                                }