Sorts JSON array value based on the specified attribute

This fiddle sorts the JSON array value based on the specified attribute

by Priyaranjan KS

HTML

<span class="SortJSON">This fiddle sorts the JSON array value based on the specified attribute</span>

CSS

.SortJSON{
    color:green;
}

JavaScript

/*
This sort method would sort the array as its not an object array . JSON array is an array of objects . Hence  sort method cannot be used directly to sort the array .

array1=["a","z","c","w","j"];
alert(array1.sort( ));*/


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#"
  }
];

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") );
document.write("Sorted Employee Names : <br>");

for (var item in array) {
 document.write("<br>"+array[item].EmployeeName);
}

array.sort( GetSortOrder("Technology") );

document.write("<br><br> Sorted Technology Names : <br>");

for (var item in array) {
 document.write("<br>"+array[item].Technology);
}