Blog Image

Sorting a two dimensional array in javascript

function sortArray(x,y)
{
return ((x[1] < y[1]) ? -1 : ((x[1] > y[1]) ? 1 : 0));
}

var givenarr = [[0,8],[1,9],[2,7]];

givenarr.sort(sortArray);

========================

The result array will be givenarr = [[2,7],[0,8],[1,9]];



Author: admin

Vinod Ram has been in Software Industry since 2006 and has experience of over 16 years in Software Development & Project Management domain specialised majorly in LAMP stack & Open Source Technology, building enterprise level Web based Application, Large Database driven and huge traffic Websites and Project Management. He loves to write information articles and blog to share his knowledge and experience with the outside world and help people to find solution for their problems.