Use String.prototype.localeCompare as per your example:

list.sort(function (a, b) {
    return ('' + a.attr).localeCompare(b.attr);
})

We force a.attr to be a string to avoid exceptions. localeCompare has been supported since Internet Explorer 6 and Firefox 1. You may also see the following code used that doesn't respect a locale:

if (item1.attr < item2.attr)
  return -1;
if ( item1.attr > item2.attr)
  return 1;
return 0;
Answer from Shog9 on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › sort
Array.prototype.sort() - JavaScript | MDN
The sort() method is generic. It only expects the this value to have a length property and integer-keyed properties. Although strings are also array-like, this method is not suitable to be applied on them, as strings are immutable.
Top answer
1 of 16
964

Use String.prototype.localeCompare as per your example:

list.sort(function (a, b) {
    return ('' + a.attr).localeCompare(b.attr);
})

We force a.attr to be a string to avoid exceptions. localeCompare has been supported since Internet Explorer 6 and Firefox 1. You may also see the following code used that doesn't respect a locale:

if (item1.attr < item2.attr)
  return -1;
if ( item1.attr > item2.attr)
  return 1;
return 0;
2 of 16
241

An updated answer (October 2014)

I was really annoyed about this string natural sorting order so I took quite some time to investigate this issue.

Long story short

localeCompare() character support is badass; just use it. As pointed out by Shog9, the answer to your question is:

return item1.attr.localeCompare(item2.attr);

Bugs found in all the custom JavaScript "natural string sort order" implementations

There are quite a bunch of custom implementations out there, trying to do string comparison more precisely called "natural string sort order"

When "playing" with these implementations, I always noticed some strange "natural sorting order" choice, or rather mistakes (or omissions in the best cases).

Typically, special characters (space, dash, ampersand, brackets, and so on) are not processed correctly.

You will then find them appearing mixed up in different places, typically that could be:

  • some will be between the uppercase 'Z' and the lowercase 'a'
  • some will be between the '9' and the uppercase 'A'
  • some will be after lowercase 'z'

When one would have expected special characters to all be "grouped" together in one place, except for the space special character maybe (which would always be the first character). That is, either all before numbers, or all between numbers and letters (lowercase & uppercase being "together" one after another), or all after letters.

My conclusion is that they all fail to provide a consistent order when I start adding barely unusual characters (i.e., characters with diacritics or characters such as dash, exclamation mark and so on).

Research on the custom implementations:

  • Natural Compare Lite https://github.com/litejs/natural-compare-lite : Fails at sorting consistently https://github.com/litejs/natural-compare-lite/issues/1 and http://jsbin.com/bevututodavi/1/edit?js,console, basic Latin characters sorting http://jsbin.com/bevututodavi/5/edit?js,console
  • Natural Sort https://github.com/javve/natural-sort : Fails at sorting consistently, see issue https://github.com/javve/natural-sort/issues/7 and see basic Latin characters sorting http://jsbin.com/cipimosedoqe/3/edit?js,console
  • JavaScript Natural Sort https://github.com/overset/javascript-natural-sort: seems rather neglected since February 2012, Fails at sorting consistently, see issue https://github.com/overset/javascript-natural-sort/issues/16
  • Alphanum http://www.davekoelle.com/files/alphanum.js , Fails at sorting consistently, see http://jsbin.com/tuminoxifuyo/1/edit?js,console

Browsers' native "natural string sort order" implementations via localeCompare()

localeCompare() oldest implementation (without the locales and options arguments) is supported by Internet Explorer 6 and later, see Legacy Microsoft Edge developer documentation (scroll down to the localeCompare() method).

The built-in localeCompare() method does a much better job at sorting, even international and special characters.

The only problem using the localeCompare() method is that "the locale and sort order used are entirely implementation dependent". In other words, when using localeCompare such as stringOne.localeCompare(stringTwo): Firefox, Safari, Chrome, and Internet Explorer have a different sort order for Strings.

Research on the browser-native implementations:

  • http://jsbin.com/beboroyifomu/1/edit?js,console - basic Latin characters comparison with localeCompare() http://jsbin.com/viyucavudela/2/ - basic Latin characters comparison with localeCompare() for testing on Internet Explorer 8
  • http://jsbin.com/beboroyifomu/2/edit?js,console - basic Latin characters in string comparison : consistency check in string vs when a character is alone
  • String.prototype.localeCompare() - Internet Explorer 11 and later supports the new locales & options arguments

Difficulty of "string natural sorting order"

Implementing a solid algorithm (meaning: consistent but also covering a wide range of characters) is a very tough task. UTF-8 contains more than 2000 characters and covers more than 120 scripts (languages).

Finally, there are some specification for this tasks, it is called the "Unicode Collation Algorithm". You can find more information about this on this question I posted, Is there a language-agnostic specification for "string natural sorting order"?

Final conclusion

So considering the current level of support provided by the JavaScript custom implementations I came across, we will probably never see anything getting any close to supporting all these characters and scripts (languages). Hence I would rather use the browsers' native localeCompare() method. Yes, it does have the downside of being non-consistent across browsers, but basic testing shows it covers a much wider range of characters, allowing solid and meaningful sort orders.

So as pointed out by Shog9, the answer to your question is:

return item1.attr.localeCompare(item2.attr);

Further reading:

  • Is there a language-agnostic specification for "string natural sorting order"?
  • How to sort strings in JavaScript
  • Natural sort of alphanumerical strings in JavaScript
  • Sort Array of numeric & alphabetical elements (Natural Sort)
  • Sort mixed alpha/numeric array
  • https://web.archive.org/web/20130929122019/http://my.opera.com/GreyWyvern/blog/show.dml/1671288
  • https://web.archive.org/web/20131005224909/http://www.davekoelle.com/alphanum.html
  • http://snipplr.com/view/36012/javascript-natural-sort/
  • Sorting for Humans : Natural Sort Order

Thanks to Shog9's nice answer, which put me in the "right" direction I believe.

Discussions

How do you sort numbers and number strings in Javascript? (From actual least to greatest
Have you read the documentation for sort()? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort sort(function compareFn(a, b) { /* … */ }) If compareFn is not supplied, all non-undefined array elements are sorted by converting them to strings and comparing strings in UTF-16 code units order. For example, "banana" comes before "cherry". In a numeric sort, 9 comes before 80, but because numbers are converted to strings, "80" comes before "9" in the Unicode order. All undefined elements are sorted to the end of the array. More on reddit.com
🌐 r/learnjavascript
8
1
September 6, 2022
Sorting a mixed array of numbers and strings
Hi there! I had a recent interview question which stumped me: Q. The following array is a mix of numbers and strings. Sort the array ascending. // Original Array: [1, 'ss', 11, 2, 'aa'] // Expected Output: [1, 2, 11, 'aa', 'ss] I can use the built-in sort function: [1, 'ss', 11, 2, 'aa'].sort() ... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
7
0
May 27, 2022
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › sort-a-string-in-javascript
Sort a String in JavaScript - GeeksforGeeks
July 23, 2025 - The most simple way to sort a string is to first convert it to an array of characters, sort the array, and then join it back into a string.
🌐
DEV Community
dev.to › elmarshall › sorting-an-array-of-number-strings-in-javascript-la7
Sorting an Array of Number Strings in JavaScript - DEV Community
May 4, 2020 - When the sort() method compares two values, it sends the values to the compare function, and sorts the values according to the returned (negative, zero, positive) value. The magic is that by performing an operation (subtraction) on the numbers ...
🌐
Mimo
mimo.org › glossary › javascript › sort
JavaScript Sort Method: Advanced Array Sorting Techniques
Sorting arrays in JavaScript is simple and versatile with the sort() method. This method allows you to sort arrays of numbers, strings, or objects with custom functions.
🌐
W3Schools
w3schools.com › js › js_array_sort.asp
JavaScript Array Sort
By default, the sort() function sorts values as strings.
Find elsewhere
🌐
LogRocket
blog.logrocket.com › home › javascript’s sort()method: from basics to custom sorting
JavaScript's sort()method: From basics to custom sorting - LogRocket Blog
March 7, 2025 - The sort and toSorted methods are built-in tools for sorting JavaScript arrays. By default, they convert elements to strings and sort them by UTF-16 code units, but you can customize their behavior using a compare function.
🌐
CoreUI
coreui.io › blog › how-to-sort-an-array-of-objects-by-string-property-value-in-javascript
How to sort an array of objects by string property value in JavaScript · CoreUI
January 18, 2025 - Sorting an array of objects by string property value in JavaScript is straightforward with the sort method and a custom comparison function.
🌐
W3Schools
w3schools.com › jsref › jsref_sort.asp
JavaScript Array sort() Method
The sort() method sorts the elements as strings in alphabetical and ascending order.
🌐
Medium
medium.com › @a1guy › sorting-in-javascript-detailed-guide-ae2c602441f9
Sorting In JavaScript — Detailed Guide | by Ali Aslam | Medium
March 21, 2025 - JavaScript’s sort() method sorts an array in place, meaning it modifies the original array rather than creating a new one. By default, it sorts elements as strings in lexicographic (dictionary) order.
🌐
DevGenius
blog.devgenius.io › how-to-sort-string-alphabetically-in-javascript-fbbb221a1ff9
How to sort string alphabetically in JavaScript ? | by Pravin M | Dev Genius
November 2, 2024 - JavaScript offers the Array.prototype.sort() method, which can be used to sort an array of strings alphabetically.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array
Array - JavaScript | MDN
July 28, 2026 - Sorts the elements of an array in place and returns the array. ... Adds and/or removes elements from an array. ... Returns a localized string representing the calling array and its elements.
🌐
Codegive
codegive.com › blog › js_sort_in_alphabetical_order.php
JS Sort Alphabetical Order: The Ultimate Guide to Perfect String Sorting (2024)
A: JavaScript's default sort() converts elements to strings and compares them based on their UTF-16 code unit values. This leads to case-sensitive sorting (e.g., 'Z' comes before 'a') and doesn't account for linguistic rules, making it unreliable for true alphabetical order.
🌐
freeCodeCamp
forum.freecodecamp.org › curriculum help
Sorting a mixed array of numbers and strings - Curriculum Help - The freeCodeCamp Forum
May 27, 2022 - Hi there! I had a recent interview question which stumped me: Q. The following array is a mix of numbers and strings. Sort the array ascending. // Original Array: [1, 'ss', 11, 2, 'aa'] // Expected Output: [1, 2, 11, 'aa', 'ss] I can use the built-in sort function: [1, 'ss', 11, 2, 'aa'].sort() ...
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › sort-an-array-of-strings-in-javascript
JavaScript - Sort an Array of Strings - GeeksforGeeks
July 12, 2025 - The sort() method is the most widely used method in JavaScript to sort arrays. By default, it sorts the strings in lexicographical (dictionary) order based on Unicode values.
🌐
Medium
medium.com › @amirakhaled2027 › javascripts-array-sort-method-3edbc7fbca22
JavaScript’s Array sort() Method
July 26, 2024 - The sort() method is an intrinsic method of the Array object in JavaScript. It sorts the elements of an array in place, modifying the original array, and returns the sorted array.
🌐
Substack
aislingmarron.substack.com › notes from new york › spring has sort of sprung
Spring has sort of sprung - by Aisling Marron
March 12, 2026 - 6 years ago, my plans for an afternoon tea with my parents, sister and 6 month old baby were cancelled right at the very last minute by *squints eyes* a global pandemic. Which we sort of saw coming yet it still totally shocked me when my brother said he didn’t think we should go.
🌐
DanyWalls
danywalls.com › understand-how-the-sort-method-operates-on-javascript-arrays
Understand How the Sort Method Operates on Javascript Arrays | DanyWalls
October 17, 2022 - MDN: The default sort order is ascending, built upon converting the elements into strings, then comparing their UTF-16 code unit value sequences.