This simple:
list.sort(function(a, b){return a.priority < b.priority;});
where list items are like {priority: 2, foo: "bar"}
Doesn't behave the same on the 2 main browsers, what am I doing wrong?
This simple:
list.sort(function(a, b){return a.priority < b.priority;});
where list items are like {priority: 2, foo: "bar"}
Doesn't behave the same on the 2 main browsers, what am I doing wrong?
You're supposed to return a numeric not a boolean in your sort() function: 1, 0 or -1. These numbers represent the direction the comparing element should travel (up, stay or down). A boolean doesn't give you this control, so most likely it's up to how the browser wants to handle it (therefore making it vary between browsers).
Check out the docs on Array.prototype.sort.
Assuming an ascending sort:
list.sort(function(a, b){
return a.priority > b.priority ? 1
: a.priority < b.priority ? -1
: 0;
});
// yields priorities of: [-8,-7,-3,0,1,3,12,29,30,200,1992]
If u like it to work in booth browsers u should change two small things.
First how u parse ur int.
list.forEach(function(data){data.priority = parseInt(data.priority,10);});
Then the sort so it returns a direction and not true/false.
list.sort(function(a, b){return b.priority - a.priority;});
This is ur fiddle but updated with the fix.
return a.priority < b.priority;
That's not a valid compare function. Returning 0
(or false
, in your case) would mean that the two items are considered equal - which they are not always. To be consistent, the compare function would need to fulfill the equation
comp(a, b) == -1 * comp(b, a)
// or, if not only the values -1, 0 and 1 are allowed:
comp(a, b) * comp(b, a) <= 0
If that requirement is broken, the sort will behave undefined.
Citing the ES5.1 spec on sort
:
If
comparefn
is […] not a consistent comparison function for the elements of this array, the behaviour of sort is implementation-defined.A function
comparefn
is a consistent comparison function for a set of valuesS
if all of the requirements below are met for all valuesa
,b
, andc
(possibly the same value) in the setS
: The notationa <CF b
meanscomparefn(a,b) < 0
;a =CF b
meanscomparefn(a,b) = 0
(of either sign); anda >CF b
meanscomparefn(a,b) > 0
.Calling
comparefn(a,b)
always returns the same valuev
when given a specific pair of valuesa
andb
as its two arguments. Furthermore,Type(v)
is Number, andv
is notNaN
. Note that this implies that exactly one ofa <CF b
,a =CF b
, anda >CF b
will be true for a given pair ofa
andb
.
- Calling
comparefn(a,b)
does not modify the this object.a =CF a
(reflexivity)- If
a =CF b
, thenb =CF a
(symmetry)- If
a =CF b
andb =CF c
, thena =CF c
(transitivity of=CF
)- If
a <CF b
andb <CF c
, thena <CF c
(transitivity of<CF
)- If
a >CF b
andb >CF c
, thena >CF c
(transitivity of>CF
)NOTE: The above conditions are necessary and sufficient to ensure that
comparefn
divides the setS
into equivalence classes and that these equivalence classes are totally ordered.
To compare numbers (reverse) in JavaScript, simply
return b.priority - a.priority;