I am new to angular js and have basic understanding of how filters work in angular js.I am stuck to sort my jSON data array as there are various parameters on which it has to get sorted.
我是角度js的新手,并且基本了解过滤器如何在角度js中工作。我很难对我的jSON数据数组进行排序,因为它有各种参数需要对它进行排序。
My JSON array is in this format:
我的JSON数组采用以下格式:
[
{
type:0,
emp_name:"xyz",
connected_on:"9876543210"
},
{
type:1,
emp_name:"",
connected_on:"9876543210"
},
{
type:1,
emp_name:"abcd",
connected_on:"9876543210"
},
{
type:0,
emp_name:"pqr",
connected_on:"9876543210"
}
]
Other combination of the same array can be:
同一阵列的其他组合可以是:
[
{
type:0,
emp_name:"",
connected_on:"9876543210"
},
{
type:1,
emp_name:"xyz",
connected_on:"9876543210"
},
{
type:0,
emp_name:"abcd",
connected_on:"9876543210"
}
]
Every array will have any one object where type:1 , will be there and after sorting it has to be the first element always.
每个数组都有任何一个对象,其中type:1,将在那里,在排序之后它必须始终是第一个元素。
After that,the sorted array should have all those elements whose emp_name:"" is there.
在那之后,排序的数组应该包含emp_name:“”的所有元素。
At last of the sorted array it should contain all the remaining elements sorted according to emp_name:"whatever names"
在排序数组的最后,它应该包含根据emp_name排序的所有剩余元素:“无论名称”
So the results should look something like this below:
所以结果应如下所示:
[
{
type:1,
emp_name:"xyz",
connected_on:"9876543210"
},
{
type:0,
emp_name:"",
connected_on:"9876543210"
},
{
type:0,
emp_name:"abcd",
connected_on:"9876543210"
}
]
Can anyone help me to write the filter to get the desired result.Thanks in advance.If any other info required please let me know.
任何人都可以帮我写过滤器以获得所需的结果。谢谢。如果需要任何其他信息,请告诉我。
Also connected_on is unique value so i am using track by JSONarr.connected_on
此外,connected_on是唯一值,因此我使用JSONarr.connected_on跟踪
2 个解决方案
#1
2
Sort function expect -1
, 0
, -1
as result. -1
means before, 0 means no change and 1 means later.
排序函数期望-1,0,-1作为结果。 -1表示之前,0表示没有变化,1表示之后。
Now you have to sort based on 2 parameters, so its better to have an arithmetic value. Have higher value based on priority. So in following situation, we have to sort first based on type
and then based on emp_name
.
现在你必须根据2个参数进行排序,因此最好有一个算术值。根据优先级获得更高的价值。因此,在以下情况中,我们必须首先根据类型排序,然后基于emp_name。
To sort ascending, return 1 for greater and -1 for smaller. For descending reverse the values. Now combining this for both keys you will get something like this:
要升序排序,请返回1表示更大,返回-1表示更小。用于降低值的反向。现在将这两个键结合使用,你会得到这样的结果:
var data=[{type:0,emp_name:"xyz",connected_on:"9876543210"},{type:1,emp_name:"",connected_on:"9876543210"},{type:1,emp_name:"abcd",connected_on:"9876543210"},{type:0,emp_name:"pqr",connected_on:"9876543210"}];
data.sort(function(a, b) {
var _a = a.type > b.type ? -10 : a.type < b.type ? 10 : 0;
var _b = a.emp_name > b.emp_name ? 1 : a.emp_name < b.emp_name ? -1 : 0;
return _a + _b;
});
console.log(data)
#2
0
Suppose your array name emplist. You can search anything here. You will get the desired result. Use table for searching the data.
假设你的数组名称为emplist。你可以在这里搜索任何东西您将获得所需的结果。使用表格搜索数据。
<input type="text" ng-model="search">
<table>
<th>type</th>
<th>Emp name</th>
<th>connected on</th>
<tr ng-repeat="item in emplist|filter:search">
<td>{{item.type}}<td>
<td>{{item.emp_name}}<td>
<td>{{item.connected_on}}<td>
</tr>
</table>
#1
2
Sort function expect -1
, 0
, -1
as result. -1
means before, 0 means no change and 1 means later.
排序函数期望-1,0,-1作为结果。 -1表示之前,0表示没有变化,1表示之后。
Now you have to sort based on 2 parameters, so its better to have an arithmetic value. Have higher value based on priority. So in following situation, we have to sort first based on type
and then based on emp_name
.
现在你必须根据2个参数进行排序,因此最好有一个算术值。根据优先级获得更高的价值。因此,在以下情况中,我们必须首先根据类型排序,然后基于emp_name。
To sort ascending, return 1 for greater and -1 for smaller. For descending reverse the values. Now combining this for both keys you will get something like this:
要升序排序,请返回1表示更大,返回-1表示更小。用于降低值的反向。现在将这两个键结合使用,你会得到这样的结果:
var data=[{type:0,emp_name:"xyz",connected_on:"9876543210"},{type:1,emp_name:"",connected_on:"9876543210"},{type:1,emp_name:"abcd",connected_on:"9876543210"},{type:0,emp_name:"pqr",connected_on:"9876543210"}];
data.sort(function(a, b) {
var _a = a.type > b.type ? -10 : a.type < b.type ? 10 : 0;
var _b = a.emp_name > b.emp_name ? 1 : a.emp_name < b.emp_name ? -1 : 0;
return _a + _b;
});
console.log(data)
#2
0
Suppose your array name emplist. You can search anything here. You will get the desired result. Use table for searching the data.
假设你的数组名称为emplist。你可以在这里搜索任何东西您将获得所需的结果。使用表格搜索数据。
<input type="text" ng-model="search">
<table>
<th>type</th>
<th>Emp name</th>
<th>connected on</th>
<tr ng-repeat="item in emplist|filter:search">
<td>{{item.type}}<td>
<td>{{item.emp_name}}<td>
<td>{{item.connected_on}}<td>
</tr>
</table>