Saturday, 15 May 2010

javascript - Sorting object by value -



javascript - Sorting object by value -

i can imagine has been asked few times literally cannot find illustration of solution specific problem i'm trying figure out.

so have object, so:

var collection = [{ id: 0 }, { id: 1 }, { id: 2 }];

i have array, 'order', so:

var order = [2, 0, 1];

i want utilize 'order' array reorder collection in specific order. i've been trying quite few solutions .sort function, can't find 1 fits. can enlighten me? simple, i'm hoping.

you can utilize sort() method accomplish using indexof:

collection.sort(function(a, b){ homecoming order.indexof(a.id) > order.indexof(b.id); });

javascript sorting

No comments:

Post a Comment