Sort array of objects by string property value in JavaScript
The array of objects can be sort by using user defined function. This function compares the array of objects by its property. For example, the first example compares the l_name of objects and if l_name is small then it place into left otherwise place it into right position.
Example 1: This example sorts the array of objects by l_name property.
<!DOCTYPE html> <html> <head> <title> Sort array of objects </title> </head> <body style = "text-align:center;"> <h1 style = "color:green;" > GeeksForGeeks </h1> <p id="demo2">GFG_Object = [ <br> { "f_name": "Geeks", "l_name": "_1" }, <br><br> { "f_name": "for", "l_name": "_2" }, <br><br> { "f_name": "GFG", "l_name": "_3" } <br>]; </p> <button onClick="fun()"> sort </button> <p id="GFG"></p> <!-- Script to compare the object and sort its content --> <script> function fun() { function compare(a, b) { if (a.l_name < b.l_name) return -1; if (a.l_name > b.l_name) return 1; return 0; } var GFG_Object = [ { f_name: 'Geeks', l_name: '_2' }, { f_name: 'for', l_name: '_1' }, { f_name: 'GFG', l_name: '_3' } ]; GFG_Object.sort(compare); document.getElementById("GFG").innerHTML = JSON.stringify(GFG_Object); } </script> </body> </html> |
Output:
-
Before click on the button:

- After click on the button:

Example 2: This example sorts the array of objects by f_name property.
<!DOCTYPE html> <html> <head> <title> Sort array of objects </title> </head> <body style = "text-align:center;"> <h1 style = "color:green;" > GeeksForGeeks </h1> <p id="demo2"> GFG_Object = [ <br> { "f_name": "Geeks", "l_name": "_1" }, <br><br> { "f_name": "for", "l_name": "_2" }, <br><br> { "f_name": "GFG", "l_name": "_3" } <br>]; </p> <button onClick="fun()"> sort </button> <p id="GFG"></p> <script> function fun() { function compare(a, b) { if (a.f_name < b.f_name) return -1; if (a.f_name > b.f_name) return 1; return 0; } var GFG_Object = [ { f_name: 'Geeks', l_name: '_2' }, { f_name: 'for', l_name: '_1' }, { f_name: 'GFG', l_name: '_3' } ]; GFG_Object.sort(compare); document.getElementById("GFG").innerHTML = JSON.stringify(GFG_Object); } </script> </body> </html> |
Output:
- Before click on the button:

- After click on the button:

Recommended Posts:
- JavaScript | length of string and array objects
- Sort array of objects by object fields in PHP
- Max/Min value of an attribute in an array of objects in JavaScript
- Extract unique objects by attribute from array of objects.
- JavaScript | Array sort()
- Sort an Object Array by Date in JavaScript
- JavaScript | String constructor Property
- JavaScript | Array length Property
- JavaScript | Array constructor Property
- Objects in Javascript
- JavaScript | Objects
- JavaScript | Date Objects
- Creating objects in JavaScript (3 Different Ways)
- How to get character array from string in JavaScript?
- How to merge properties of two JavaScript objects dynamically?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



