Javascript 按属性对对象数组进行排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36667731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 19:23:12  来源:igfitidea点击:

Sorting array of objects by property

javascriptangularjsangularjs-filter

提问by Michael

I have this collection from 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 中是否有任何内置函数可以按属性对对象数组进行排序?

回答by Pankaj Parkar

You could use orderByfilter.

你可以使用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 -TypeIdto make it descending.

默认情况下,过滤器是升序的(显式是+TypeId),您可以使用-TypeId它降序。



Additional Stuff

额外的东西

If you wanted to sort by multiple properties then do use array instead of stringlike ['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-repeatexpress and bindings each time when digest cycle fire. Generally you won't see any performance hit in small collection, but in bigger collection you will see filtering on view will work slow.

当您在控制器内部进行手动过滤时,您将获得巨大的性能优势。其中视图过滤速度较慢,因为它ng-repeat每次在摘要循环触发时都会评估express 和 bindings。通常,您不会在小集合中看到任何性能下降,但在较大的集合中,您会看到视图过滤工作缓慢。

回答by brk

You can use js sortfunction & ternary operator

您可以使用 jssort函数 &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 示例

回答by Ori Drori

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>

回答by GG.

In a template:

在模板中:

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

In a controller/service:

在控制器/服务中:

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