3
function longest(arr) {
  return arr.sort( (a,b) => {
    return a.length - b.length;
  });
}
var res = longest(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'k','l']);
console.log(res); // -> [ 'f', 'a', 'c', 'd', 'e', 'b', 'g', 'h', 'i', 'k', 'l' ]

How could the return of this function be sorting in such a wierd way? As I understand, it should not change the array at all.

Am I correct?

RickBrunstedt
  • 1,921
  • 2
  • 12
  • 12

4 Answers4

10

Array.protoype.sort is not guaranteed to be stable. No guarantees are made that equal elements will retain their original order.

The sort() method sorts the elements of an array in place and returns the array. The sort is not necessarily stable. The default sort order is according to string Unicode code points.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

Community
  • 1
  • 1
deceze
  • 510,633
  • 85
  • 743
  • 889
3

You can add a default sorting with the position of the item of original array. (For better performance use Sorting with map.)

function longest(arr) {
    var copy = arr.slice();
    return arr.sort(function (a, b) {
        return a.length - b.length || copy.indexOf(a) - copy.indexOf(b);
    });
}
var res = longest(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'k', 'l']);
document.write('<pre>' + JSON.stringify(res, 0, 4) + '</pre>');
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

If you want to sort by ascending it should be the comparator function should return a - b not a.length - b.length because a.length - b.length the result will always be zero for all the alphabets of length 1. The result will be different for ['ab', 'a'] will produce ['a', 'ab'] because the length of first element in array is lesser than second element.

function longest(arr) {
  return arr.sort( (a,b) => {
    return a - b;
  });
}
fusion3k
  • 11,568
  • 4
  • 25
  • 47
Dhananjaya Kuppu
  • 1,322
  • 9
  • 10
  • This would sort them i alphabetic order. I want to sort them by length. – RickBrunstedt Mar 30 '16 at 09:04
  • Could you give an example input and output to my knowledge whatever you have written will work fine. If I understand correctly by length you mean number of characters in a string then longest(['ab', 'a']) will result in ['a', 'ab'] but all the strings that you gave in input is having only one character so the result will have no order. – Dhananjaya Kuppu Mar 30 '16 at 09:28
  • But it doesn't work fine. when they are same length, nothing should happen, but it does. It rearrange, but it shouldn't. In my real problem when I noticed this some strings was one character long, some wasn't. – RickBrunstedt Apr 01 '16 at 13:09
0

As stated here:

If compareFunction(a, b) returns 0, leave a and b unchanged with respect to each other, but sorted with respect to all different elements. Note: the ECMAscript standard does not guarantee this behaviour, and thus not all browsers (e.g. Mozilla versions dating back to at least 2003) respect this.

Keeping in mind that the return of the compare function in your example will always be 0, and also refering to the answers in this topic you can see how different browsers itterate over elements differently. Therefore the results of your case are to be expected and probably varry from browser to browser

Community
  • 1
  • 1