javascript - Sorting a JSON object excluding an item -
i have json object follows:
[ {"at&t" : "blocked"}, {"all" : "targeted"}, {"verizon" : "blocked"}, {"sprint" : "blocked"} ]
which sort alphabetically using following function:
sortbykey : function(array, key) { return array.sort(function(a, b) { var x = a[key]; var y = b[key]; return ((x < y) ? -1 : ((x > y) ? 1 : 0)); }); },
this works fine. want exclude {"all" : "targeted"} sorting , have first element of json object so:
[ {"all" : "targeted"}, {"at&t" : "blocked"}, {"sprint" : "blocked"} {"verizon" : "blocked"}, ]
could give me hint of on how can achieved? i'll need way exclude item json object sorting , set first element of object.
thanks in advance!
if want exclude value, eg keep @ top of sort, whenever sort function encounters return -1 if first argument or 1 if second
//keep 2 @ front [1,254,6,2,62].sort(function(a,b){ if(a == 2) return -1; else if(b == 2) return 1; return a-b; }); [2, 1, 6, 62, 254] //keep 2 @ [1,254,6,2,62].sort(function(a,b){ if(a == 2) return 1; else if(b == 2) return -1; return a-b; }); [1, 6, 62, 254, 2]
Comments
Post a Comment