Monday, 04 Aug 2008 18:55 EST

Sorting 2D Arrays in JavaScript

So you're working with a 2D array in JavaScript and you need to find a way to sort the damn thing. As it turns out, it's not nearly as bad as you might think. While JavaScript does not have a built in 2D array sort function to go along with array.sort(), you are able to utilize good old sort() by passing it a custom comparison function. Let start with the following 2D array:
var customers = new Array();
customers[0] = {lastName:"Williams", firstName:"Tom", company:"ABC Inc"};
customers[1] = {lastName:"Smith", firstName:"John", company:"123 Ltd"};
customers[2] = {lastName:"Johnson", firstName:"Bob", company:"Johnson Co"};
By default, customers.sort() will sort on the first field defined in the array, lastName. Let's say we want to sort on company, though. All we need to do is create a custom comparison function which will grab the value of company, compare the two and return 1 if a.company is higher in the order and -1 if b.company is higher. Very simple, yet very powerful.
function sortByCompany(a, b) {
  var x = a.company;
  var y = b.company;
  return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}
Now you simply call JavaScript's standard array sort function with the name of your comparison function as the only parameter.
customers.sort(sortByCompany);
To sort descending, simply swap 1 and -1 in the comparison function.
function sortByCompanyDESC(a, b) {
  var x = a.company;
  var y = b.company;
  return ((x < y) ? 1 : ((x > y) ? -1 : 0));
}

customers.sort(sortByCompanyDESC);
As always, if you have a technique that you think is better or a tweak that improves on this code, please share it in the comments. If you have a tip, a trick or an idea for my next post, please don't hesitate to contact me.
Bookmark and Share

3 Comments So Far

Patrick
Sunday, 26 October 2008 3:33
Great script!! Saved my day!Just wondering how it would be possible to sort by company and then by last name?thanksPatrick
Wednesday, 06 January 2010 03:05
Monday, 15 February 2010 09:50

Post a comment


If you are seeing this message, you are either using a screen reader or you have disabled CSS. Please do not fill in the following form field, titled "lastname", or your comment will not be saved. This field is only used to help prevent spam comments.


Welcome

JustinSpegele.com is where I share projects that I'm working on, php tutorials, web development tips and tricks, and random thoughts.

#MLS has a very odd notion of an all-star game. MLS all-stars vs. an EPL team? Still, should be very fun to watch. MLS 3 - Man United 2 1:41 PM Jul 28th from web
The New Digg And The Future Of Social News http://bit.ly/cM7Guq 10:44 AM Jun 29th from TweetMeme
so as of right now, the Big 10 has 12 teams and the Big 12 has 10 teams? http://sports.espn.go.com/ncaa/news/story?id=5276668 7:59 PM Jun 11th from web
heading to Las Vegas for #caworld in the morning 7:12 PM May 12th from web
Found out from CNN that I went to high school with a convicted terrorist who worked with Al-Qaeda. Not what I expected to see on the news 9:49 PM May 10th from web
View all posts on Twitter »

Friend me

Twitter Digg Facebook FriendFeed Del.icio.us iFanboy Last.fm Squidoo

Login

Username:

Password: