1

I have multiple arrays. Lets take three for example: long, lati, and depth:

var long = [3, 4, 6, 5, 7, 8];
var lati = [2.2, 4.3, 0.5, 1.3, 2.3, 1.1];
var depth = [1, 5, 2, 6, 4, 3];

So depth of long (3) & lati (2.2) is '1'.

3    2.2   1
4    4.3   5
6    0.5   2
5    1.3   6
7    2.3   4
8    1.1   3

I want to sort depth, and based on that, long and lati should get sorted respectively. So final result should be:

var long = [3, 6, 8, 7, 4, 5];
var lati = [2.2, 0.5, 1.1, 2.3, 4.3, 1.3];
var depth = [1, 2, 3, 4, 5, 6];
Ansari Khalid
  • 57
  • 1
  • 11

1 Answers1

1

Would you consider an alternate data structure? I think that would end up being the way to go. I would recommend something like the following:

const lLDArray = [];
for(let i = 0; i < long.length(); i++) {
lLDArray.push({long: long[i], lat: lati[i], depth: depth[i]});
}
// Sort the array based on callback function
lLDArray.sort((a, b) => a.depth - b.depth);
CodeoftheWarrior
  • 311
  • 2
  • 12
  • Also, based on your usage of var and not const and let, I may recommend that you start using ES6 if you're not already. https://www.w3schools.com/js/js_es6.asp – CodeoftheWarrior Jan 29 '21 at 05:09