按字母顺序对对象数组进行排序

时间:2019-01-21 21:51:04

标签: arrays angular typescript sorting

我正在尝试按字母顺序对对象数组进行排序,为了使事情变得简单,我使用以下示例。在我的打字稿中,我进行拼接以从数组对象中插入和删除项目。

数组

  cars = [{
        id: 1,
        items: [{
          name: 'car1',
          description: 'this is car1 description'
        },{
          name: 'car2',
          description: 'this is car2 description'
        },{
          name: 'car3',
          description: 'this is car3 description'
        },{
          name: 'car4',
          description: 'this is car4 description'
        },{
          name: 'car5',
          description: 'this is car5 description'
        }]
   }];

html

<p-dataView [value]="cars" [paginator]="true" [rows]="5">
  <p-header>List of Cars</p-header>
  <p-footer>Choose from the list.</p-footer>
  <ng-template let-car pTemplate="listItem">
      <p-fieldset legend="Header" *ngIf="car.id === 1" [toggleable]="true">
          <div *ngFor="let _car of car.items">
              {{_car.name}} - {{_car.description}}
          </div>
      </p-fieldset>
  </ng-template>
</p-dataView>

************************************************更新************************************ 抱歉,我还有一层

  cars = [{
        id: 1,
        items: [{
             Model:[{  
                        name: 'car1',
                        description: 'this is car1 description'
                   }],
             Model:[{  
                        name: 'car2',
                        description: 'this is car2 description'
                   }],

        },
           id:2,
            items: [{

               Model:[{  
                        name: 'car13,
                        description: 'this is car3 description'
                   }],
             Model:[{  
                        name: 'car4',
                        description: 'this is car4 description'
                   }],
        }]
   }];

我尝试了

cars[0].items.Model.sort((a,b) => a[0].name > b[0].name ? 1 : -1) //It did not work

也尝试过

cars[0].items.Model.sort(function (a, b) {
                var nameA = a.name.toLowerCase();
                var nameB = b.name.toLowerCase();
                if (nameA < nameB) //sort string ascending
                    return -1
            })

4 个答案:

答案 0 :(得分:1)

我发现将Underscore.js之类的库用于实用程序功能最为有效:

Underscore.js,尤其是sortBy方法。

将underscorejs添加到您的项目中,并将其导入到您的组件中。

import * as _ from 'underscore';

然后调用sort方法并传递对象数组和键进行排序。

_.sortBy(car.items, 'name');

当您在car阵列中添加或删除一项时,请使用car.items集合并将其重新分配给car

car.items = _.sortBy(car.items, 'name');

然后,您可以显示当前数据以及已排序的数据。

答案 1 :(得分:1)

使用Array.prototype.sort()

进行自然排序
cars.sort(function(x,y) {
  if (x.items.name < y.items.name) return -1;
  if (x.items.name > y.items.name) return 1;
  return 0;
})

答案 2 :(得分:1)

或者我们可以简化为:

 cars[0].items.sort((a,b) => a.name.localeCompare(b.name))

答案 3 :(得分:0)

cars = [{
        id: 1,
        items: [{
          name: 'ab',
          description: 'this is car1 description'
        },{
          name: 'cd',
          description: 'this is car2 description'
        },{
          name: 'car3',
          description: 'this is car3 description'
        },{
          name: 'aaa',
          description: 'this is car4 description'
        },{
         name: 'car5',
          description: 'this is car5 description'
        }]
   }];

  cars[0].items.sort((a,b) => a.name > b.name ? 1 : -1)