Javascript, how do you sort an array on multiple columns?

Posted by flavour404 on Stack Overflow See other posts from Stack Overflow or by flavour404
Published on 2010-05-06T20:27:04Z Indexed on 2010/05/06 22:18 UTC
Read the original article Hit count: 195

Filed under:

Hi,

I have a multidimensional array, the primary array is an array of [publicationID][publication_name][ownderID][owner_name]. What I am trying to do is sort the array by owner name and then by publication_name. I know in JavaScript you have Array.sort(), into which you can put a custom function, in my case i have:

function mysortfunction(a, b) {
var x = a[3].toLowerCase();
var y = b[3].toLowerCase();

return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}

This is fine for just sorting on the one column, namely owner_name, but how do I modify it to sort on owner_name, then publication_name?

Thanks, R.

© Stack Overflow or respective owner

Related posts about JavaScript