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 OverflowMDN 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.
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
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
[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
"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
Top answer 1 of 16
2039
Shortest possible code with ES6!
users.sort((a, b) => a.firstname.localeCompare(b.firstname))
String.prototype.localeCompare() basic support is universal!
2 of 16
1510
Suppose you have an array users. You may use users.sort and pass a function that takes two arguments and compare them (comparator)
It should return
- something negative if first argument is less than second (should be placed before the second in resulting array)
- something positive if first argument is greater (should be placed after second one)
- 0 if those two elements are equal.
In our case if two elements are a and b we want to compare a.firstname and b.firstname
Example:
users.sort(function(a, b){
if(a.firstname < b.firstname) { return -1; }
if(a.firstname > b.firstname) { return 1; }
return 0;
})
This code is going to work with any type.
Note that in "real life"™ you often want to ignore case, correctly sort diacritics, weird symbols like ß, etc. when you compare strings, so you may want to use localeCompare. See other answers for clarity.
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
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
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 ...