Introduction
JavaScript has a native Sort method that can be used to sort arrays. However, based on the nature of the array, the implementation varies.
- data=["a","z","c","w","j"];
- alert (data.sort( ));
Comparer function has the following format:
- function compare(a, b) {
- if (a is less than b by some ordering criterion) {
- return -1;
- }
- if (a is greater than b by the ordering criterion) {
- return 1;
- }
- // a must be equal to b
- return 0;
- }
- If ComparingFunction(a, b) is < 0, then a will be sorted to a lower index than b. Thus a will come first.
- If ComparingFunction (a, b) returns 0, then a and b will retain their respective positions. There won’t be any change in sort order.
- If ComparingFunction (a, b) is > then 0, then b will be sorted to a lower index than a. Thus b will come first.
The comparer function can be called to sort the JSON array as below:
- var array = [{
- "EmployeeName": "John",
- "Experience": "12",
- "Technology": "SharePoint"
- }, {
- "EmployeeName": "Charles",
- "Experience": "9",
- "Technology": "ASP.NET"
- }, {
- "EmployeeName": "Jo",
- "Experience": "3",
- "Technology": "JAVA"
- }, {
- "EmployeeName": "Daine",
- "Experience": "7",
- "Technology": "Sql Server"
- }, {
- "EmployeeName": "Zain",
- "Experience": "6",
- "Technology": "C#"
- }];
- //Comparer Function
- function GetSortOrder(prop) {
- return function(a, b) {
- if (a[prop] > b[prop]) {
- return 1;
- } else if (a[prop] < b[prop]) {
- return -1;
- }
- return 0;
- }
- }
- array.sort(GetSortOrder("EmployeeName")); //Pass the attribute to be sorted on
- document.write("Sorted Employee Names : ");
- for (var item in array) {
- document.write("<br>" + array[item].EmployeeName);
- }
- array.sort(GetSortOrder("Technology")); //Pass the attribute to be sorted on
- document.write("<br><br> Sorted Technology Names : ");
- for (var item in array) {
- document.write("<br>" + array[item].Technology);
- }
Charles
Daine
Jo
John
Zain
ASP.NET
C#
JAVA
SharePoint
Sql Server
You can play around with the JSFiddle I have created here for better understanding.
Reference

Ashik AshikPosted Apr 16, 2018, 2:12 AM
In case, i sort array by Experience. the result should sort by Experience. emp John showing me first. bt in the core function John should show the last one
ketan patelPosted Feb 21, 2018, 6:13 AM
Super cool. worked great for me. Just twiked it for asc and desc.
Asfend YarPosted Feb 27, 2016, 3:57 AM
Nice Share
Santhakumar MunuswamyPosted Oct 25, 2015, 11:53 AM
Good one
Mohammed IbrahimPosted Oct 19, 2015, 10:04 PM
nice
Priyaranjan K SPosted Oct 19, 2015, 12:47 PM
Thanks Humayun Kabir Mamun
Priyaranjan K SPosted Oct 19, 2015, 12:47 PM
Thanks Gowtham K
Priyaranjan K SPosted Oct 19, 2015, 12:46 PM
Thanks for reading Mukesh Kumar
Priyaranjan K SPosted Oct 19, 2015, 12:46 PM
Thank You Sibeesh Venu
Priyaranjan K SPosted Oct 19, 2015, 12:46 PM
Thanks Nilesh Jadav
Gowtham KPosted Oct 19, 2015, 12:15 PM
Good Share
Sibeesh VenuPosted Oct 19, 2015, 8:52 AM
Nice Share
Mukesh KumarPosted Oct 19, 2015, 8:16 AM
Good One
Humayun Kabir MamunPosted Oct 19, 2015, 8:11 AM
Nice...
Nilesh JadavPosted Oct 19, 2015, 8:08 AM
Nice one sir !!
Priyaranjan K SPosted Oct 19, 2015, 8:08 AM
Thanks Harshad Pansuriya
Harshad PansuriyaPosted Oct 19, 2015, 8:06 AM
Nice one