0

I am getting a NAN error when i am using this code to sort a JSON array.

  people.sort(function(a, b) {
     a = a.title.toLowerCase();
   b = b.title.toLowerCase();

  return a-b;
  })
shashank
  • 101
  • 6
  • 1
    It's just an array, not a "Json array." JSON is a *textual notation* for data exchange. [(More here.)](http://stackoverflow.com/a/2904181/157247) If you're dealing with JavaScript source code, and not dealing with a *string*, you're not dealing with JSON. – T.J. Crowder Feb 06 '18 at 09:46
  • 1. `NaN` is not an error, it's just a value. 2. If you're seeing `NaN` from `a-b`, it means either `a` or `b` (which are both strings, the result of `toLowerCase`) cannot be coerced to a number. 3. There's no point in making a string lower case before coercing it to a number; there is no meaningful capitalization in numbers. – T.J. Crowder Feb 06 '18 at 09:47
  • Plus, converting to upper is a preferred way of doing the comparison. – CRice Feb 06 '18 at 09:49

0 Answers0