0

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?

http://jsfiddle.net/8NCXr/1/

enter image description here

3 Answers3

2

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]
Brad Christie
  • 100,477
  • 16
  • 156
  • 200
  • 1
    Returning booleans is actually fine, as they will be converted to `0` or `1`. However, that doesn't form a valid ordering, and different sorting algorithms will lead to different incorrect results. – Bergi Dec 06 '13 at 14:27
0

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.

0
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 values S if all of the requirements below are met for all values a, b, and c (possibly the same value) in the set S: The notation a <CF b means comparefn(a,b) < 0; a =CF b means comparefn(a,b) = 0 (of either sign); and a >CF b means comparefn(a,b) > 0.

Calling comparefn(a,b) always returns the same value v when given a specific pair of values a and b as its two arguments. Furthermore, Type(v) is Number, and v is not NaN. Note that this implies that exactly one of a <CF b, a =CF b, and a >CF b will be true for a given pair of a and b.

  • Calling comparefn(a,b) does not modify the this object.
  • a =CF a (reflexivity)
  • If a =CF b, then b =CF a (symmetry)
  • If a =CF b and b =CF c, then a =CF c (transitivity of =CF)
  • If a <CF b and b <CF c, then a <CF c (transitivity of <CF)
  • If a >CF b and b >CF c, then a >CF c (transitivity of >CF)

NOTE: The above conditions are necessary and sufficient to ensure that comparefn divides the set S into equivalence classes and that these equivalence classes are totally ordered.

To compare numbers (reverse) in JavaScript, simply

return b.priority - a.priority;
Bergi
  • 630,263
  • 148
  • 957
  • 1,375