2

Array sort function behaves weird after the length of array grows bigger than 10.pasted code below

var keys = [ 
      'CHECKSUMHASH',
      'CUST_ID',
      'EMAIL',
      'TEST',
      'INDUSTRY_TYPE_ID',
      'REQUEST_TYPE',
      'CHANNEL_ID',
      'MOBILE_NO',
      'ORDER_ID',
      'payt_STATUS'
       ]

    keys.sort(function(a,b){ return a.toLowerCase() > b.toLowerCase()})

    outputs 
    [ 'CHANNEL_ID',
      'CHECKSUMHASH',
      'CUST_ID',
      'EMAIL',
      'INDUSTRY_TYPE_ID',
      'MOBILE_NO',
      'ORDER_ID',
      'payt_STATUS',
      'REQUEST_TYPE',
      'TEST' ]

But adding one extra string to array and making length of array greater than 10 behaves weird.

var keys = [ 
  'CHECKSUMHASH',
  'CUST_ID',
  'EMAIL',
  'TEST',
  'INDUSTRY_TYPE_ID',
  'REQUEST_TYPE',
  'CHANNEL_ID',
  'MOBILE_NO',
  'ORDER_ID',
  'payt_STATUS',
  'a'
   ]

keys.sort(function (a, b) {
        return a.toLowerCase() > b.toLowerCase();
    });
["REQUEST_TYPE", "a", "CHANNEL_ID", "CHECKSUMHASH", "CUST_ID", "EMAIL", "INDUSTRY_TYPE_ID", "MOBILE_NO", "ORDER_ID", "payt_STATUS", "TEST"]

i expect 'a' to come first than 'REQUEST_TYPE'. Please explain this behaviour.

1
  • 1
    the expected result of sort is not true or false, it is -1 0 1. You will get problems in IE with it. Anyway the result don't seems to be ordered right? I expected 'a' comes first, but REQUEST_TYPE comes before TEST. Commented Nov 14, 2015 at 9:30

2 Answers 2

1

You're returning an invalid value from the sort callback. The callback should return 0 if the arguments are equal, a negative number if the first arg is "smaller" than the first, and a positive number if the first is "bigger" than the second.

String#localeCompare can give you the correct value:

var keys = [
  'CHECKSUMHASH',
  'CUST_ID',
  'EMAIL',
  'TEST',
  'INDUSTRY_TYPE_ID',
  'REQUEST_TYPE',
  'CHANNEL_ID',
  'MOBILE_NO',
  'ORDER_ID',
  'payt_STATUS',
  'a'
]

keys.sort(function(a, b) {
  return a.toLowerCase().localeCompare(b.toLowerCase());
});
snippet.log(JSON.stringify(keys));
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="//tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

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

2 Comments

Thanks TJ Crowder, But any idea why after 10 array length it fails.And true false 1 0 are treated boolean same.
@sudhirbaru: It doesn't fail after 10 if you write it correctly, and again, the callback for sort doesn't just have return values true/false or 1/0. It's any negative number, 0, or any positive number. (Three possible values, not two.)
1

Your sorting function is incorrect, try something like this instead:

keys.sort(function (a, b) {
    if (a.toLowerCase() > b.toLowerCase())
        return 1;
    else if (a.toLowerCase() < b.toLowerCase())
        return -1;
    else return 0;
});

EDIT: as @T.J. Crowder mentioned, the sorting function should return a positive number(1) if the first argument is bigger, a negative (-1) if its smaller, or 0 if they are equal; not true or false. In this case it seems that using the localeCompare method he suggested might be better since localeCompare would cope with different languages if you ever need to, and saves you writing some code.

1 Comment

"as T.J. Crowder mentioned, the sorting function should return 1 if the first argument is bigger, -1 if its smaller, or 0 if they are equal; not true or false" No, I said 0, a negative number, or a positive number. It's an important difference. Sorting an array of numbers in reverse order, for instance, is simply .sort(function(a, b) { return b - a; }).

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.