I've got an array of objects, each of which has a property name
, a string. I'd like to sort the array by this property. I'd like them sorted in the following way..
`ABC`
`abc`
`BAC`
`bac`
etc...
How would I achieve this in JavaScript?
I've got an array of objects, each of which has a property
How would I achieve this in JavaScript? |
||||
add comment |
There are 2 basic ways:
or
Be aware that the 2nd version ignore diacritics, so Now the problem with both these ways is that they will not sort uppercase To fix that, you will have to do it like this:
Again here you could choose to use
You can read more about sort here: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/sort |
|||||
|
You can pass-in a sort function reference to Array.sort. |
|||||
|
see there http://jsfiddle.net/p8Gny/1/ |
|||||||||
|
You can pass a custom sorting function to the
|
|||||||||||||
|
Slightly modified from Sorting an array of objects,
|
||||
|
array.sort()
? It does exactly what you want. – Raynos Mar 24 '11 at 15:13name
property of each object, whichsort()
will not do without a custom function. – Tim Down Mar 24 '11 at 15:26