[Solved] javascript or PHP sorting array of x,y coordinates


Use sort with a custom compare function:

data.sort(function(a, b){
  return a[1] - b[1];
});

Example:

var data = [[4,1],[20,1],[66,1],[68,3],[70,3],[72,2],[84,1],[96,4],[102,2]];
data.sort(function(a, b){
  return a[1] - b[1];
});
// data now contains:

   [[4,1],[20,1],[66,1],[84,1],[72,2],[102,2],[68,3],[70,3],[96,4]];

If you want to sort in descending order instead just do b[1] - a[1] instead.

5

solved javascript or PHP sorting array of x,y coordinates