Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: Sort multi dimensional Array based on first arrays sort results

I have an array setup similar to this:

var ary1 = new Array("d", "a", "b", "c");
var ary2 = new Array("ee", "rr", "yy", "mm");

var mdAry = new Array(ary1, ary2);

ary1 and ary2 indexes are info related to each other in the grand scheme of things.

d ee
a rr
b yy
c mm

I can sort() ary1 and get:

a
b
c
d

but if I sorted ary2 independently I would get:

ee
mm
rr
yy

which visually breaks ary1 and ary2 connections when listed out. Can I retrieve ary1's sorted solution and apply that to ary2? I want to get this:

a rr
b yy
c mm
d ee

If not, could mdAry be sorted so that it applies mdAry[0] sorted solution to the remaining indicies?

like image 981
David Torno Avatar asked May 23 '13 01:05

David Torno


3 Answers

If your array items are related, then store them together:

var arr = [
  {x: 'd', y: 'ee'}, 
  {x: 'a', y: 'rr'}, 
  {x: 'b', y: 'yy'},
  {x: 'c', y: 'mm'}
];

arr.sort(function(a, b) {
  if (a.x != b.x) {
      return a.x < b.x ? -1 : 1;
  }
  return 0;
});
like image 110
Ja͢ck Avatar answered Oct 20 '22 13:10

Ja͢ck


One way to do this is to transform the data structure to something that can be sorted more easily, and then transform it back after

var ary1  = ["d", "a", "b", "c"],
    ary2  = ["ee", "rr", "mm", "yy"]
    mdAry = [ary1, ary2];

// convert to form [[d, ee], [a, rr], ..]
var tmp = mdAry[0].map(function (e, i) {
    return [e, mdAry[1][i]];
});
// sort this
tmp.sort(function (a, b) {return a[0] > b[0];});
// revert to [[a, b, ..], [rr, mm, ..]]
tmp.forEach(function (e, i) {
    mdAry[0][i] = e[0];
    mdAry[1][i] = e[1];
});
// output
mdAry;
// [["a", "b", "c", "d"], ["rr", "mm", "yy", "ee"]]
like image 1
Paul S. Avatar answered Oct 20 '22 11:10

Paul S.


Just to add yet another method in there, you could get a sort "result" from the first array and apply that to any other related list:

function getSorter(model) {
    var clone = model.slice(0).sort();
    var sortResult = model.map(function(item) { return clone.indexOf(item); });

    return function(anyOtherArray) {
        result = [];
        sortResult.forEach(function(idx, i) {
            result[idx] = anyOtherArray[i];
        });
        return result;
    }
}

Then,

var arr = ["d", "a", "b", "c"];
var arr2 = ["ee", "rr", "yy", "mm"];

var preparedSorter = getSorter(arr);
preparedSorter(arr2); 
//=> ["rr", "yy", "mm", "ee"];

Or,

multidimensional = [arr, arr2];
multidimensional.map(getSorter(arr)); 
// => [["a", "b", "c", "d"], ["rr", "yy", "mm", "ee"]]
like image 1
matehat Avatar answered Oct 20 '22 12:10

matehat