Sort Array Both Ascending And Descending Using Jquery
I am trying to sort my array, both ascending and descending. I cant seem to figure out how to do this correctly though. I tried with different methods, but none worked. This is su
Solution 1:
As you're sorting an array of arrays rather than sorting just a series of names, you'll need to do something slightly different for sorting.
Try using something such as this:
function arrSort(arr, subkey){
//Default to 0 if no subkey is set
subkey = (subkey===undefined?0:subkey);
var a = arr.slice(0),
b = [], x;
//For each section in the array, create an array containing whatever we are trying to sort by and our unique ID
for (x in a){
b[x]=[a[x][subkey], x];
}
b=b.sort();
//Wipe out all the data that's currently in arr!
arr.splice(0, arr.length);
for (x in b){
arr.push(a[b[x][1]]);
}
return arr;
}
I believe you'd be wanting to sort by $titel so you'd want to implement this something like this:
// kör funktionen när knappen med id stigande klickas
$('#stigande').click(function(a,b){
arrSort($films, 0);
});
// kör funktionen när knappen med id fallande klickas
$('#fallande').click(function(){
arrSort($films, 0);
$films.reverse();
});
Post a Comment for "Sort Array Both Ascending And Descending Using Jquery"