Yangshun Tay
Yangshun Tay

Reputation: 53169

Why does this incorrect sorting (a comparator that returns boolean) work in Firefox?

I understand that this behaviour is due to return 0 in the comparator, my question is about why this wrong code still worked in Firefox. Please read the whole question before answering.

I have this set of data:

var data = [
  'Salas Mccarthy',
  'Jodi Graham',
  'Hollie Hardin',
  'Bullock Cole',
  'Peterson Mosley',
  'Lucille Jackson',
  'Cooper Leonard',
  'Iris Shepherd',
  'Oneil Head',
  'Swanson Singleton',
  'Sutton Odom'
]

console.log(data.sort(function (a, b) { return a > b; }));

Running that code in Chrome browser gives me a non-sorted result:

["Lucille Jackson", "Salas Mccarthy", "Hollie Hardin", "Bullock Cole", "Peterson Mosley", "Jodi Graham", "Cooper Leonard", "Iris Shepherd", "Oneil Head", "Sutton Odom", "Swanson Singleton"]

while running that same code in Firefox gives me a sorted result:

[ "Bullock Cole", "Cooper Leonard", "Hollie Hardin", "Iris Shepherd", "Jodi Graham", "Lucille Jackson", "Oneil Head", "Peterson Mosley", "Salas Mccarthy", "Sutton Odom", "Swanson Singleton"]

My understanding is that if I returned a > b like I did, true/false gets converted to 1/0 and 0 means leave it unchanged with respect to each other. Hence I understand that Chrome gives me a non-sorted result with the incorrect comparator.

I should have returned a numeric value in the comparator function, such as

data.sort(function (a, b) { return a > b ? 1 : -1; })

Doing so will give consistent sorted results in both browsers. But I'm still puzzled why it works in Firefox in the first place?

Afterwards, I removed Salas Mccarthy from the array. Both Chrome and Firefox sorts the array nicely even with the incorrect comparator!

var data = [
  'Jodi Graham',
  'Hollie Hardin',
  'Bullock Cole',
  'Peterson Mosley',
  'Lucille Jackson',
  'Cooper Leonard',
  'Iris Shepherd',
  'Oneil Head',
  'Swanson Singleton',
  'Sutton Odom'
]

console.log(data.sort(function (a, b) { return a > b }));

Results for both Chrome and Firefox:

["Bullock Cole", "Cooper Leonard", "Hollie Hardin", "Iris Shepherd", "Jodi Graham", "Lucille Jackson", "Oneil Head", "Peterson Mosley", "Sutton Odom", "Swanson Singleton"]

These sorting behaviours are really really odd, can anyone explain it?

Upvotes: 1

Views: 165

Answers (2)

brk
brk

Reputation: 50326

You can use localeCompare method to returns a number indicating whether a reference string comes before or after or is the same as the given string in sort order

var data = [
  'Salas Mccarthy',
  'Jodi Graham',
  'Hollie Hardin',
  'Bullock Cole',
  'Peterson Mosley',
  'Lucille Jackson',
  'Cooper Leonard',
  'Iris Shepherd',
  'Oneil Head',
  'Swanson Singleton',
  'Sutton Odom'
]

console.log(data.sort(function(a, b) {
  return a.localeCompare(b);
}));

Upvotes: 0

springfeld
springfeld

Reputation: 89

Use [].sort() without callback-function on an array of strings, if all you want is a lexigraph sorting: sort() is first and foremost a unicode lexigraph sort function, if not used with a callback function.

Upvotes: 0

Related Questions