0

I have one array. I need to sort that array using lodash. I used _.sortBy but it is not sorting capital string, why ?

Here is my code: https://jsfiddle.net/2q9cdjug/

var arr = [
    { name: "pq" },
    { name: "ab" },
    { name: "QE" }  
]
var a = _.sortBy(arr, 'name');
console.log(a)
Rory McCrossan
  • 319,460
  • 37
  • 290
  • 318
user944513
  • 11,195
  • 34
  • 140
  • 266
  • Possible duplicate of [Sorting an array of JavaScript objects](http://stackoverflow.com/questions/979256/sorting-an-array-of-javascript-objects) – Starfish May 04 '16 at 06:53
  • Possible duplicate of [Underscore.js Case Insensitive Sorting](http://stackoverflow.com/questions/25873635/underscore-js-case-insensitive-sorting) – Aides May 04 '16 at 06:54

2 Answers2

2

But it is not sorting capital string why?

Because 'a' !== 'A'

You can use callback function, So you can sort them by converting to same cases using either toLowerCase() or toUpperCase()

a = _.sortBy(arr, function (x) {
    return x.name.toLowerCase();
});

DEMO

Satpal
  • 129,808
  • 12
  • 152
  • 166
0

This is because of the ascii table of characters

edit: javascript uses utf-16, and not the table below (utf-8), but it is a good example to understand because utf-16 table is long

enter image description here

a is 97 and A is 65

if you try console.log("a" > "A") //true sorting strings should be in lowercase

Daniel Krom
  • 9,233
  • 3
  • 38
  • 43