In java script we can do sorting using the sort() function. The default sort() function is designed to sort Strings / Numbers. But how do we sort objects using our own custom sort() function.
To do custom sorting on an array of objects, you can pass a your own sorting function as the argument to sort() function. Following a code snippet which shows how to use your custom sorting function. Proper comments are provided for each line to understand the code better. The same method can be can be used for JSON objects because there is no difference between Java Script objects and JSON objects
var employeeArray = [];
//Creating 3 employee objects and adding it to an array
var employee = {};
employee.employeeId = 10;
employee.employeeName = "employee-3";
employee.department = 2;
employeeArray.push(employee);
employee = {};
employee.employeeId = 11;
employee.employeeName = "employee-2";
employee.department = 3;
employeeArray.push(employee);
employee = {};
employee.employeeId = 4;
employee.employeeName = "employee-4";
employee.department = 7;
employeeArray.push(employee);
//Sorting the array based on employee id. See sort function below
employeeArray.sort(sortByEmployeeId)
for(i=0; i<employeeArray.length; i++)
{
alert("Sort by EmployeeId: " + employeeArray[i].department + "-" +
employeeArray[i].employeeId + "-" +
employeeArray[i].employeeName);
}
//Sorting the array based on employee name. See sort function below
employeeArray.sort(sortByEmployeeName);
for(i=0; i<employeeArray.length; i++)
{
alert("Sort by Employee Name: " + employeeArray[i].department + "-" +
employeeArray[i].employeeId + "-" +
employeeArray[i].employeeName);
}
//Sorting the array based on department and employee id. See sort function below
employeeArray.sort(sortByDepartmentAndEmployeeId);
for(i=0; i<employeeArray.length; i++)
{
alert("Sort by Department > Employee Id: " +
employeeArray[i].department + "-" +
employeeArray[i].employeeId + "-" +
employeeArray[i].employeeName);
}
//Sort function to sort employees based on employee id
function sortByEmployeeId(a,b)
{
//return 1, if you want b to come first
//return -1, if you want a to come first
//return 0, if both objects are the same
if(a.employeeId < b.employeeId)
{
return 1;
}
else if(a.employeeId > b.employeeId)
{
return -1;
}
else
{
return 0;
}
/*
//This can be written in short as below
return a.employeeId - b.employeeId;
*/
}
//Sort function to sort employees based on employee name
function sortByEmployeeName(a,b)
{
//return 1, if you want b to come first
//return -1, if you want a to come first
//return 0, if both objects are the same
//java script supports <, > and = on Strings.
//Strings will be compared lexically
if(a.employeeName < b.employeeName)
{
return 1;
}
else if(a.employeeName > b.employeeName)
{
return -1;
}
else
{
return 0;
}
}
//Sort function to sort employees based on
//department first and employee id second
function sortByDepartmentAndEmployeeId(a,b)
{
//return 1, if you want b to come first
//return -1, if you want a to come first
//return 0, if both objects are the same
if(a.department < b.department)
{
return 1;
}
else if(a.department > b.department)
{
return -1;
}
else if(a.employeeId < b.employeeId)
{
return 1;
}
else if(a.employeeId > b.employeeId)
{
return -1;
}
else
{
return 0;
}
}