Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

3. Arrays and Objects > 3.11. Sorting an Array of Objects

Sorting an Array of Objects

Problem

You want to sort an array of objects based on the value of one of the properties of the objects.

Solution

Sorting an array of objects relies on a logical extension of the comparison function described for simple arrays in Sorting a Simple Array. 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.

To demonstrate the concept, we’ll start with the array of sales objects:

	var sales = new Array();
	sales[sales.length] = {period:"q1", region:"east", total:2300};
	sales[sales.length] = {period:"q2", region:"east", total:3105};
	...
	sales[sales.length] = {period:"q4", region"west", total:3810};

  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free 10-Day Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint