0

I have a question about array.sort() method in JavaScript. Set of values in the array can vary from 1 to more. Sort function should sort them in the order if there is more than one element. Here is my code:

var myDates = ["03/05/2017","02/01/2017","03/02/2017"];

myDates.sort(function(a,b) {
    a = a.split('/').reverse().join('');
    b = b.split('/').reverse().join('');
    return a > b ? 1 : a < b ? -1 : 0;
});

Code above works fine, all dates are sorted. My question is should I check the length of the array before I run the sort method? I'm asking this because my array can have one element only in some situations. So far my code did not throw any errors when I tested with one element only, but I would like to know if I should check the length of the array before I run the sort() or JavaScript already takes care of that? If anyone knows the answer please let me know. Thank you.

4
  • well ... check what happens yourself, for both 0 and 1 element - hint, put a console.log inside the callback Commented Mar 8, 2017 at 3:03
  • How that will help me to get the answer? I know what my function will produce if I have more than one element but I'm wondering if I should prevent my function running if I have less than two elements. Commented Mar 8, 2017 at 3:10
  • fine ... if there's less than 2 elements, the sort callback is not even called - so, there's no need for you to check the length. Honestly, I just thought you may want to learn how to learn Commented Mar 8, 2017 at 3:12
  • I tried but console.log() did not produce any output inside of the call back function. Thanks for explaining. Commented Mar 8, 2017 at 3:31

1 Answer 1

1

This behaviour is documented in the Array.prototype.sort specification. See http://www.ecma-international.org/ecma-262/6.0/#sec-array.prototype.sort

Specifically:

The arguments for calls to SortCompare are values returned by a previous call to the [[Get]] internal method, unless the properties accessed by those previous calls did not exist according to HasOwnProperty. If both perspective arguments to SortCompare correspond to non-existent properties, use +0 instead of calling SortCompare. If only the first perspective argument is non-existent use +1. If only the second perspective argument is non-existent use −1.

In short:

Array.prototype.sort((undefined, undefined) => { ... }); // => 0

Array.prototype.sort((undefined, b) => { ... }); // => 1

Array.prototype.sort((a, undefined) => { ... }); // => -1

Sign up to request clarification or add additional context in comments.

2 Comments

No, it is relevant. If both perspective arguments to SortCompare correspond to non-existent properties, use +0 instead of calling SortCompare...
sorry, I see what you're saying - your code used undefined which confused me - will remove my "critique" :p

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.