Shortest possible code with ES6!

users.sort((a, b) => a.firstname.localeCompare(b.firstname))

String.prototype.localeCompare() basic support is universal!

Answer from Nachiketha on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › sort
Array.prototype.sort() - JavaScript - MDN Web Docs
The sort() method of Array instances sorts the elements of an array in place and returns the reference to the same array, now sorted. The default sort order is ascending, built upon converting the elements into strings, then comparing their sequences of UTF-16 code unit values.
🌐
W3Schools
w3schools.com › jsref › jsref_sort.asp
JavaScript Array sort() Method
The sort() method sorts the elements of an array. The sort() method sorts the elements as strings in alphabetical and ascending order.
Discussions

Sort array by firstname (alphabetically) in JavaScript - Stack Overflow
The last parameter is of boolean type. It checks if you want to sort by ascending or by descending ... Save this answer. ... Show activity on this post. ... Save this answer. ... Show activity on this post. ... Wrong wrong wrong! You don't get a valid result if you subtract strings in Javascript. More on stackoverflow.com
🌐 stackoverflow.com
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
[AskJS] Why does the Sort method function the way it does?
Honestly it's just because someone early on decided it to be this way and now it cannot be changed. Python could have implemented their sort the same as JS, or JS could have implemented it the same as Python. They just happened to make different decisions. This behavior is consistent with how JavaScript types normally function. When performing operations between two different types, the types are both converted to the "lowest common denominator" type that both can be safely converted to. This is most often a string. This is the same behavior if you try to check if a number is greater than a string, or add a number to a string. They'll get implicitly converted to strings first. Same thing is happening here when comparing elements of different types. More on reddit.com
🌐 r/javascript
8
8
January 13, 2022
"How to sort arrays in Javascript"

Don't worry, I got you! ref

let array = [1,100_000, 21, 30, 4];  
array.sort((a,b) => a-b)
More on reddit.com
🌐 r/ProgrammerHumor
188
2487
September 8, 2020
🌐
DEV Community
dev.to › banesag › sorting-arrays-of-strings-in-javascript-2g11
JavaScript Sort Array: Sorting Arrays of Strings in JavaScript - DEV Community
December 13, 2018 - In JavaScript arrays have a sort( ) method that sorts the array items into an alphabetical order.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › sort-an-array-of-strings-in-javascript
JavaScript - Sort an Array of Strings - GeeksforGeeks
July 12, 2025 - sort() sorts the array a in place. In the case of strings, it compares the Unicode values of the characters.
🌐
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.
🌐
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 - To recap the previous section, the JavaScript sort() array method takes an optional compare function as a callback. If you don’t pass a callback, the array’s elements will be coerced to strings and are sorted in ascending order of the numerical values of their UTF-16 code units.
Find elsewhere
🌐
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.
🌐
Medium
paperinflames.medium.com › sort-array-of-strings-in-javascript-a6a20b6ba1fb
Sort Array of Strings in JavaScript | by PaperInFlames | Medium
November 18, 2023 - Another way is using the inbuilt methods of a string in JS. Here we have to swap the values for ascending or descending orders. In the below example, we have to swap a and b. // Sort in ascending order let arr = ['K38', 'K23', 'K89']; let res= arr.sort((a,b) => a.localeCompare(b)); console.log(res) //Output : ["K23", "K38", "K89"] // Sort in descending order let arr = ['K38', 'K23', 'K89']; let res= arr.sort((a,b) => b.localeCompare(a)); console.log(res) //Output : ["K89", "K23", "K38"]
🌐
Futurestud.io
futurestud.io › tutorials › sort-an-array-of-strings-in-javascript-typescript-or-node-js
Sort an Array of Strings in JavaScript, TypeScript or Node.js
September 1, 2022 - JavaScript comes with the Array#sort method. This sort method allows you to provide a comparator callback.
🌐
GeeksforGeeks
geeksforgeeks.org › sort-an-array-of-strings-in-javascript
JavaScript – Sort an Array of Strings | GeeksforGeeks
December 3, 2024 - Using Array.sort() MethodThe 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.JavaScriptlet a = ['Banana',
🌐
YouTube
youtube.com › tapascript by tapas adhikary
How To Sort An Alphanumeric String Array In JavaScript? - YouTube
JavaScript is changing rapidly with the inclusion of modern APIs that help in the easiest way of handling things. In this video, we will discuss one such met...
Published: October 1, 2024
Views: 660
🌐
OpenReplay
blog.openreplay.com › openreplay blog › array and string sorting recipes for javascript
Array and String Sorting recipes for JavaScript
August 8, 2022 - Using .sort(...) to work with an array of strings is simple because you don’t have to do anything at all! If no function is supplied, sorting is done by comparing the string representations of the array values.
🌐
W3Schools
w3schools.com › js › js_array_sort.asp
JavaScript Array Sort
JavaScript arrays often contain objects: const cars = [ {type:"Volvo", year:2016}, {type:"Saab", year:2001}, {type:"BMW", year:2010} ]; Even if objects have properties of different data types, the sort() method can be used to sort the array.
🌐
YouTube
youtube.com › watch
How to Sort a String Array In JavaScript (Ascending and Descending Order) - YouTube
How to Sort a String Array In JavaScript (Simple)Greetings, in this quick Javascript tutorial, we shall be looking at how to sort String arrays in ascending ...
Published: March 17, 2023
🌐
freeCodeCamp
freecodecamp.org › news › how-to-sort-javascript-array-accurately
JavaScript Sort Array - How to Sort an Array Accurately
November 7, 2024 - The sort() method of JavaScript compares the elements of the array by converting them into strings and then comparing their Unicode code points.
🌐
JavaScript Tutorial
javascripttutorial.net › home › javascript array methods › array.prototype.some()
JavaScript Array sort: Sorting an Array More Effectively
November 8, 2024 - The sort() method sorts array elements in place and returns the sorted array. The sort() method converts numbers into strings before sorting.
🌐
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 ...
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › sort-array-of-objects-by-string-property-value-in-javascript
Sort Array of Objects By String Property Value in JavaScript - GeeksforGeeks
July 11, 2025 - const a = [ { s: "apple", price: 10 }, { s: "banana", price: 12 }, { s: "apple", price: 8 } ]; a.sort((x, y) => x.s.localeCompare(y.s) || x.price - y.price); console.log(a); ... For applications having multiple languages, Intl.Collator offers ...
🌐
Will Vincent
wsvincent.com › javascript-sort-array
JavaScript: Sort an Array of Strings or Numbers - Will Vincent
January 2, 2018 - const numArr = [1, 5, 0, 11, 25, 40]; numArr.sort(); // [0, 1, 11, 25, 40, 5] numArr.sort((a,b) => a - b); // [0, 1, 5, 11, 25, 40] numArr.sort((a,b) => b - a); // [40, 25, 11, 5, 1, 0] Want to improve your JavaScript?