如何通过angularjs中的id数组对对象列表进行排序?

时间:2020-11-29 07:41:59

I'm trying to sort my list of arrays, but to no avail.

我正在尝试对数组列表进行排序,但无济于事。

The problem: I have a list of objects however I need to sort it by an id string, which is inside an array.

问题:我有一个对象列表,但我需要通过一个id字符串对它进行排序,它在一个数组中。

JSON:

[{"id":2613,"name":"Aula 01","section":989},{"id":2614,"name":"Aula 02","section":989},{"id":2616,"name":"Aula 04","section":989},{"id":2617,"name":"Aula 05","section":989,},{"id":2615,"name":"3 - Aula 03","section":989}]

Array id sequence:

数组ID序列:

["2613", "2614", "2615", "2616", "2617"]

How to order ng-repeat or a loop in this array?

如何在这个数组中命令ng-repeat或循环?

5 个解决方案

#1


2  

If you need an arbitrary order of the given id with the sequence array, you could use an object with the order for sorting.

如果您需要使用序列数组的给定id的任意顺序,则可以使用具有排序顺序的对象。

array = [{ id: 2613, name: "Aula 01", section: 989 }, { id: 2614, name: "Aula 02", section: 989 }, { id: 2616, name: "Aula 04", section: 989 }, { id: 2617, name: "Aula 05", section: 989 }, { id: 2615, name: "3 - Aula 03", section: 989 }],
    sequence = ["2617", "2613", "2614", "2615", "2616"],
    order = {};

sequence.forEach(function (id, i) { order[id] = i + 1 });

array.sort(function (a, b) { return order[a.id] - order[b.id]; });

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

#2


1  

Sorting with ng-repeat is really quite easy

使用ng-repeat进行排序非常简单

<div data-ng-repeat="item in items | orderBy: 'id' ">

#3


0  

You could sort like this a.id-b.id using js

你可以使用js这样排序a.id-b.id

var arr =[{"id":2613,"name":"Aula 01","section":989},{"id":2614,"name":"Aula 02","section":989},{"id":2616,"name":"Aula 04","section":989},{"id":2617,"name":"Aula 05","section":989,},{"id":2615,"name":"3 - Aula 03","section":989}]
var res = arr.sort((a,b)=> a.id-b.id)
console.log(res)

#4


0  

There's a couple ways to do this. 1. You could write a sorting function in your controller that sorts the return object after the api is called and put in a variable to reference in angular after it has been sorted. 2. You can use something like this: orderBy array item value in Angular ng-repeat on ng repeat.

有几种方法可以做到这一点。 1.您可以在控制器中编写一个排序函数,在调用api之后对返回对象进行排序,并在排序之后放入一个变量以引用角度。 2.你可以使用这样的东西:在ng重复的Angular ng-repeat中的orderBy数组项值。

#5


0  

You can use the the native sort method and use compare function, like this:

您可以使用本机排序方法并使用比较功能,如下所示:

var json = [{"id":2613,"name":"Aula 01","section":989},{"id":2614,"name":"Aula 02","section":989},{"id":2616,"name":"Aula 04","section":989},{"id":2617,"name":"Aula 05","section":989,},{"id":2615,"name":"3 - Aula 03","section":989}];

var result = json.sort((a, b) => a.id - b.id)

console.log(result);

Assuming your JSON is coming from a service, it might look like this:

假设您的JSON来自服务,它可能如下所示:

$scope.json = service.getJson().sort((a, b) => a.id - b.id);

Or maybe:

service.getJson().then(function (data) {

   $scope.json = data.sort((a, b) => a.id - b.id);
});

Then you can just ng-repeat it in the view.

然后你可以在视图中重复它。

#1


2  

If you need an arbitrary order of the given id with the sequence array, you could use an object with the order for sorting.

如果您需要使用序列数组的给定id的任意顺序,则可以使用具有排序顺序的对象。

array = [{ id: 2613, name: "Aula 01", section: 989 }, { id: 2614, name: "Aula 02", section: 989 }, { id: 2616, name: "Aula 04", section: 989 }, { id: 2617, name: "Aula 05", section: 989 }, { id: 2615, name: "3 - Aula 03", section: 989 }],
    sequence = ["2617", "2613", "2614", "2615", "2616"],
    order = {};

sequence.forEach(function (id, i) { order[id] = i + 1 });

array.sort(function (a, b) { return order[a.id] - order[b.id]; });

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

#2


1  

Sorting with ng-repeat is really quite easy

使用ng-repeat进行排序非常简单

<div data-ng-repeat="item in items | orderBy: 'id' ">

#3


0  

You could sort like this a.id-b.id using js

你可以使用js这样排序a.id-b.id

var arr =[{"id":2613,"name":"Aula 01","section":989},{"id":2614,"name":"Aula 02","section":989},{"id":2616,"name":"Aula 04","section":989},{"id":2617,"name":"Aula 05","section":989,},{"id":2615,"name":"3 - Aula 03","section":989}]
var res = arr.sort((a,b)=> a.id-b.id)
console.log(res)

#4


0  

There's a couple ways to do this. 1. You could write a sorting function in your controller that sorts the return object after the api is called and put in a variable to reference in angular after it has been sorted. 2. You can use something like this: orderBy array item value in Angular ng-repeat on ng repeat.

有几种方法可以做到这一点。 1.您可以在控制器中编写一个排序函数,在调用api之后对返回对象进行排序,并在排序之后放入一个变量以引用角度。 2.你可以使用这样的东西:在ng重复的Angular ng-repeat中的orderBy数组项值。

#5


0  

You can use the the native sort method and use compare function, like this:

您可以使用本机排序方法并使用比较功能,如下所示:

var json = [{"id":2613,"name":"Aula 01","section":989},{"id":2614,"name":"Aula 02","section":989},{"id":2616,"name":"Aula 04","section":989},{"id":2617,"name":"Aula 05","section":989,},{"id":2615,"name":"3 - Aula 03","section":989}];

var result = json.sort((a, b) => a.id - b.id)

console.log(result);

Assuming your JSON is coming from a service, it might look like this:

假设您的JSON来自服务,它可能如下所示:

$scope.json = service.getJson().sort((a, b) => a.id - b.id);

Or maybe:

service.getJson().then(function (data) {

   $scope.json = data.sort((a, b) => a.id - b.id);
});

Then you can just ng-repeat it in the view.

然后你可以在视图中重复它。