javascript sort and sort equals on result. how?
Sorry my bad English Has problem in sort.
I has objects something, is structure like this: {"name", "count"}.
So, I had massive of objects
for example:
name => "aaa", count => 1
name => "bbb", count => 2
name => "ccc", count => 3
name => "ddd", count => 1
name => "eee", count => 1
I need to sort this objects of "count" parameter (small to big, then big
to small). I use my function (code in end of this message)
and on first call (small to big) I get one result, this result is res1 on
seccond call (big to small) I get second result, this result is res2 on
third call (small to big again) I get third result this result is res3
Problem: res1 !== res3
res1 and res3 has no similar elements order (becouse equal values has
another order in another call). I need they has similar order.
I hope you are understand my problem.
Please, help!
my functions:
mass.sort(compareElements);
function compareElements(a, b)
{
if(a < b) return -1;
else if(a > b) return 1;
else return 0;
}
No comments:
Post a Comment