arrays - Filter to sort in angular js for JSON data -
i new angular js , have basic understanding of how filters work in angular js.i stuck sort json data array there various parameters on has sorted.
my json array in format:
[ { 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 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 have 1 object type:1 , there , after sorting has first element always.
after that,the sorted array should have elements emp_name:"" there.
at last of sorted array should contain remaining elements sorted according emp_name:"whatever names"
so results should 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 me write filter desired result.thanks in advance.if other info required please let me know.
also connected_on unique value using track jsonarr.connected_on
sort function expect -1
, 0
, -1
result. -1
means before, 0 means no change , 1 means later.
now have sort based on 2 parameters, better have arithmetic value. have higher value based on priority. in following situation, have sort first based on type
, based on emp_name
.
to sort ascending, return 1 greater , -1 smaller. descending reverse values. combining both keys this:
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)
Comments
Post a Comment