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 15, 2018, 10:12 PM EDT
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, 1:13 AM EST
Super cool. worked great for me. Just twiked it for asc and desc.
Asfend YarPosted Feb 26, 2016, 10:57 PM EST
Nice Share
Santhakumar MunuswamyPosted Oct 25, 2015, 7:53 AM EDT
Good one
Mohammed IbrahimPosted Oct 19, 2015, 6:04 PM EDT
nice
Priyaranjan K STOP 100Posted Oct 19, 2015, 8:47 AM EDT
Thanks Humayun Kabir Mamun
Priyaranjan K STOP 100Posted Oct 19, 2015, 8:47 AM EDT
Thanks Gowtham K
Priyaranjan K STOP 100Posted Oct 19, 2015, 8:46 AM EDT
Thanks for reading Mukesh Kumar
Priyaranjan K STOP 100Posted Oct 19, 2015, 8:46 AM EDT
Thank You Sibeesh Venu
Priyaranjan K STOP 100Posted Oct 19, 2015, 8:46 AM EDT
Thanks Nilesh Jadav
Gowtham KTOP 100Posted Oct 19, 2015, 8:15 AM EDT
Good Share
Sibeesh VenuTOP 100Posted Oct 19, 2015, 4:52 AM EDT
Nice Share
Mukesh KumarPosted Oct 19, 2015, 4:16 AM EDT
Good One
Humayun Kabir MamunPosted Oct 19, 2015, 4:11 AM EDT
Nice...
Nilesh JadavPosted Oct 19, 2015, 4:08 AM EDT
Nice one sir !!
Priyaranjan K STOP 100Posted Oct 19, 2015, 4:08 AM EDT
Thanks Harshad Pansuriya
Harshad PansuriyaPosted Oct 19, 2015, 4:06 AM EDT
Nice one