简体   繁体   English

按属性排序对象数组

[英]Sorting array of objects by property

I have this collection from DataBase: 我有来自DataBase的这个集合:

var items = [{ 'Name':'Michael', 'TypeId':1 }
        { 'Name':'Max', 'TypeId':1 }
        { 'Name':'Andre', 'TypeId':1 }
        { 'Name':'Georg', 'TypeId':2 }
        { 'Name':'Greg', 'TypeId':3 }
        { 'Name':'Mitchell', 'TypeId':2 }
        { 'Name':'Ptro', 'TypeId':1 }
        { 'Name':'Helga', 'TypeId':1 }
        { 'Name':'Seruin', 'TypeId':2 }
        { 'Name':'Ann', 'TypeId':3 }
        { 'Name':'Marta', 'TypeId':2 }]

I need to sort this items by TypeId increasing. 我需要通过TypeId增加对这些项目进行排序。

Like that: 像那样:

var itemsSorted = [{ 'Name':'Michael', 'TypeId':1 }
        { 'Name':'Max', 'TypeId':1 }
        { 'Name':'Andre', 'TypeId':1 }
        { 'Name':'Ptro', 'TypeId':1 }
        { 'Name':'Helga', 'TypeId':1 }
        { 'Name':'Georg', 'TypeId':2 }
        { 'Name':'Mitchell', 'TypeId':2 }
        { 'Name':'Marta', 'TypeId':2 }]
        { 'Name':'Seruin', 'TypeId':2 }
        { 'Name':'Greg', 'TypeId':3 }
        { 'Name':'Ann', 'TypeId':3 }

Is there any built in function in JavaScript that can sort array of objects by property? JavaScript中是否有内置函数可以按属性对对象数组进行排序?

You could use orderBy filter. 您可以使用orderBy过滤器。

var itemsSorted  = $filter('orderBy')(items, 'TypeId')

On View 在视图上

ng-repeat="item in items | orderBy: 'TypeId'"

By default filters are ascending(explicit would be +TypeId ), you could use -TypeId to make it descending. 默认情况下,过滤器是升序的(显式为+TypeId ),您可以使用-TypeId使其降序。


Additional Stuff 额外的东西

If you wanted to sort by multiple properties then do use array instead of string like ['TypeId', 'Name'] 如果你想按多个属性排序,那么使用数组代替string['TypeId', 'Name']

ng-repeat="item in items | orderBy: ['TypeId', 'Name']"

There is big performance benefit you get when you do manual filtering inside controller. 在控制器内部进行手动过滤时,可以获得很大的性能优势。 where as filtering on view is slower as it evaluates ng-repeat express and bindings each time when digest cycle fire. 其中视图上的过滤速度较慢,因为每次消化周期触发时,它都会评估ng-repeat表达和绑定。 Generally you won't see any performance hit in small collection, but in bigger collection you will see filtering on view will work slow. 通常,您不会在小型集合中看到任何性能损失,但在更大的集合中,您将看到对视图的过滤将变得缓慢。

You can use js sort function & ternary operator 您可以使用js sort函数和ternary operator

var items = [{ 'Name':'Michael', 'TypeId':1 },
            { 'Name':'Max', 'TypeId':1 },
            { 'Name':'Andre', 'TypeId':1 },
            { 'Name':'Georg', 'TypeId':2 },
            { 'Name':'Greg', 'TypeId':3 },
            { 'Name':'Mitchell', 'TypeId':2 },
            { 'Name':'Ptro', 'TypeId':1 },
            { 'Name':'Helga', 'TypeId':1 },
            { 'Name':'Seruin', 'TypeId':2 },
            { 'Name':'Ann', 'TypeId':3 },
            { 'Name':'Marta', 'TypeId':2 }]
    var sortedArray = items.sort(function(a,b){
     return a.TypeId >b.TypeId?1:a.TypeId <b.TypeId?-1:0
    })
    console.log(sortedArray);

JSFIDDLE Example JSFIDDLE示例

You can use Array.prototype.sort : 您可以使用Array.prototype.sort

 var items = [{ 'Name':'Michael', 'TypeId':1 }, { 'Name':'Max', 'TypeId':1 }, { 'Name':'Andre', 'TypeId':1 }, { 'Name':'Georg', 'TypeId':2 }, { 'Name':'Greg', 'TypeId':3 }, { 'Name':'Mitchell', 'TypeId':2 }, { 'Name':'Ptro', 'TypeId':1 }, { 'Name':'Helga', 'TypeId':1 }, { 'Name':'Seruin', 'TypeId':2 }, { 'Name':'Ann', 'TypeId':3 }, { 'Name':'Marta', 'TypeId':2 }]; items.sort(function(a, b) { return a.TypeId - b.TypeId; }) console.table(items); document.getElementById('demo').innerHTML = JSON.stringify(items, null, 4); 
 <pre id="demo"></pre> 

In a template: 在模板中:

<li ng-repeat="item in items | orderBy:'TypeId'">...</li>

In a controller/service: 在控制器/服务中:

vm.sortedItems = $filter('orderBy')(items, 'TypeId');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM