2

Say for example, I have an Array:

[[1,2,3,4,5,6],
["Red", "Blue", "Green", "Yellow", "Black", "Sliver"],
[25, 50, 10, 0, 5, 100]]

How would I sort this data into ascending order of row 3?

For example

[[4,5,3,1,2,6],
["Yellow", "Black", "Green", "Red", "Blue", "Sliver"],
[0, 5, 10, 25, 50, 100]]
2
  • Please show what have you done so far and refer stackoverflow.com/help/how-to-ask Commented Jan 29, 2017 at 12:56
  • can you explain how the second subarray is ordered? Commented Jan 29, 2017 at 13:10

4 Answers 4

2

var myArray = [
  [1, 2, 3, 4, 5, 6],
  ["Red", "Blue", "Green", "Yellow", "Black", "Sliver"],
  [25, 50, 10, 0, 5, 100]
];

// Giving an array arr swap the values of indexes i and j
function swap(arr, i, j) {
  var temp = arr[i];
  arr[i] = arr[j];
  arr[j] = temp;
}

// the array which will be sorted you can choose anyone here (more flexible that way)
var theArr = myArray[2];

// sort theArr
for (var i = 0, len = theArr.length; i < len - 1; i++) {
  // assume minIndex is the first
  var minIndex = i;
  // check if there is something lesser than whatever in minIndex
  for (var j = minIndex + 1; j < len; j++) {
    // if so ...
    if (theArr[minIndex] > theArr[j])
    // minIndex becomes this index (j)
      minIndex = j;
  }

  // now that we found our minIndex lets swap minIndex value with i value for all the sub-arrays of myArray
  myArray.forEach(function(subArr) {
    swap(subArr, minIndex, i);
  });
}

console.log(myArray);

NOTE: myArray can have as much sub-arrays as you want (they just have to be of the same size of course). And you can sort according to whatever sub-array you want (var theArr = myArray[/*desired sub-array index*/]; (It's more flexible that way).

Sign up to request clarification or add additional context in comments.

Comments

1

There's a standard function in many languages called zip that converts an array of rows into an array of columns, for example, if a is

[
    [1, 2, 3],
    [4, 5, 6]
] 

then zip(a) will be:

[ [1, 4], [2, 5], [3, 6] ]

JS has no built-in zip, but it's trivial to write:

let zip = a => a[0].map((_, i) => a.map(r => r[i]));

With zip your problem can be solved in one line:

let result = zip(zip(data).sort((a, b) => a[2] - b[2]));

Basically, convert your rows to columns, sort these by the 3rd element and then convert back to rows.

3 Comments

please can you provide some links for zip.? it is really interesting.
yea really nice concept.mapping a tuple of sequences into a sequence of tuples.Thank you.
0

First sort the last array to get the sorted map, then use this map to sort the corresponding other two arrays.

Modified @Nenad code to get the expected output.

let arr = [
  [1, 2, 3, 4, 5, 6],
  ["Red", "Blue", "Green", "Yellow", "Black", "Sliver"],
  [25, 50, 10, 0, 5, 100]
]

let sortBy = arr[2].slice().sort((a, b) => a - b).reduce((r, e, i) =>{
  r[i] = arr[2].indexOf(e)
  return r;
}, {})


let x = arr.map( (e)  =>{
   return( e.slice().sort( (a, b) =>{
    return sortBy[e.indexOf(a)] - sortBy[e.indexOf(b)]
  })
  )
})

console.log(JSON.stringify(x))

Comments

0

You can first take order of last array form data that you want to sort by and put it in object and then sort by that object.

var arr = [
  [1, 2, 3, 4, 5, 6],
  ["Red", "Blue", "Green", "Yellow", "Black", "Sliver"],
  [25, 50, 10, 0, 5, 100]
]

var sortBy = arr[2].slice().sort((a, b) => a - b).reduce(function(r, e, i) {
  r[i] = arr[2].indexOf(e)
  return r;
}, {})

arr.forEach(function(e, i) {
  arr[i] = e.slice().sort(function(a, b) {
    return sortBy[e.indexOf(a)] - sortBy[e.indexOf(b)]
  })
})

console.log(JSON.stringify(arr))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.