Javascript Sorting an Array of Objects
|
|
|
|
| Articles Reviews Javascript | |
| Written by Larry Gomez | |
| Friday, 09 March 2007 | |
|
If you want to sort an array of objects based on the value of one of the properties of the objects can use the next solution: Define a comparison function as usual, but let the actual comparisons work on the properties of the objects being passed to the function two at a time. We'll start with the array of sales objects: var sales = new Array( ); If you want to sort the sales array in descending order of the values of the total properties of each object, define a comparison function that returns the appropriate values based on the arithmetic: function compareTotals(a, b) { Because each array entry passed as parameters a and b is an object, you can use those parameter variables to reference the properties of the objects as they pass through in waves during the full sort operation. To sort the array by way of the comparison function, pass the function's reference to the sort( ) method of the array: sales.sort(compareTotals); Recall that the sort( ) method modifies the order of the original array. But you can invoke other sort( ) methods (that call other comparison functions) to resort the array by other criteria. Comparison functions can get rather elaborate if necessary. It all depends on the kind of data in your object properties and what kind of sorting you need to perform. For example, if an object is defined with separate properties for month, day, and year, and if you want to sort the objects by the dates that those numbers represent, the comparison function can create date objects from those values and then compare the resulting date objects: function compareDates(a, b) { If sorting is required of string values in properties, you have to be more explicit in the comparisons you perform and the values you return. You may also want to eliminate case as a factor by comparing values converted to all upper- or all lowercase characters. The following function sorts string values of the lastName property of an array of objects: function compareNames(a, b) { Because the toLowerCase( ) method of a string doesn't disturb the case of the original string, the object values are ready to be displayed as entered into the data structure.
Average user rating from: 7 user(s)
Add new review 1 of 1 people found the following review helpful
Beautiful!!!, Wednesday, 02 November 2011 Written by Yuriy
Report this review
1 of 1 people found the following review helpful
Sort Function, Wednesday, 05 October 2011 Written by Scott function compareNames(a, b) { var nameA = a.lastName.toLowerCase( ); var nameB = b.lastName.toLowerCase( ); if (nameA nameB) {return 1} return 0; }
Report this review
1 of 4 people found the following review helpful
OMG! Thank you!, Friday, 12 August 2011 Written by Peter Worked like a charm!
Report this review
0 of 1 people found the following review helpful
blew me away...., Thursday, 28 April 2011 Written by thomas
Report this review
0 of 0 people found the following review helpful
So simple and clean!, Tuesday, 08 February 2011 Written by Bill
Report this review
4 of 10 people found the following review helpful
Simple Understanding, Monday, 01 February 2010 Written by Nanda Kumar fast understanding example that has been provided. can u explain the concept for return b.total - a.total; here how substract is taking place.. thankx in advance, Nanda
Report this review
13 of 16 people found the following review helpful
Very Nice, Have long been thinking about that one, Thursday, 29 January 2009 Written by Nils Daniel Wittwer Thx!
Report this review
Powered by jReviews |
|
| Last Updated ( Tuesday, 28 June 2011 ) | |
| < Prev | Next > |
|---|









