Is this possible to sort one array according to the sorting order obtained by sorting another array?

Viewed 65

Here I have two arrays in javascript code

const array1 = [200, 455.23, -306.5, 9000, -642.21, -133.9, 79.97, 1300];
const array2 = [
    '2019-11-18T21:31:17.178Z',
    '2019-12-23T07:42:02.383Z',
    '2020-01-28T09:15:04.904Z',
    '2020-04-01T10:17:24.185Z',
    '2022-04-08T14:11:59.604Z',
    '2022-04-10T17:01:17.194Z',
    '2022-04-11T23:36:17.929Z',
    '2022-04-13T10:51:36.790Z',
];
array1.sort((a, b) => a - b);

Here I want array2 to be sorted exactly in the same order by which array1 is sorted. suppose that every element of both arrays at the same index has to be at the same index even after sorting array1. In short, I want to sort one array. And I want the other array sorted in exact same order.

3 Answers

Here goes an example of how to implement the suggestion above, where both values are in one single array, like objects, and sorted by the value of each object in the array.

const myArray = [
  {date: '2019-11-18T21:31:17.178Z', value: 200},
  {date: '2019-12-23T07:42:02.383Z', value: 455.23},
  {date: '2020-01-28T09:15:04.904Z', value: -306.5},
  {date: '2020-04-01T10:17:24.185Z', value: 9000},
  {date: '2022-04-08T14:11:59.604Z', value: -642.21},
  {date: '2022-04-10T17:01:17.194Z', value: -133.9},
  {date: '2022-04-11T23:36:17.929Z', value: 79.97 },
  {date: '2022-04-13T10:51:36.790Z', value: 1300},
]

myArray.sort((a, b) => a.value - b.value)
console.log(myArray);

First create an array of object literals, that contains the value pairs. Then use the sortby key to sort that array. You are done!

const array1 = [200, 455.23, -306.5, 9000, -642.21, -133.9, 79.97, 1300];
const array2 = ['2019-11-18T21:31:17.178Z','2019-12-23T07:42:02.383Z','2020-01-28T09:15:04.904Z','2020-04-01T10:17:24.185Z','2022-04-08T14:11:59.604Z','2022-04-10T17:01:17.194Z','2022-04-11T23:36:17.929Z','2022-04-13T10:51:36.790Z',];

const sorted = array1.map((a1,index)=>{return {sortBy: a1, value: array2[index]}}).sort((a,b)=>a.sortBy-b.sortBy);
console.log(sorted);

//-------------------------------------------------------
//or in a function

const sortArrayByAnother = (arr, sortBy) => {
  return sortBy.map((a1,index)=>{return {sortBy: a1, value: arr[index]}}).sort((a,b)=>a.sortBy-b.sortBy).map((d)=>d.value);
}

console.log(sortArrayByAnother(array2, array1));

To sort 2 or more arrays without combining them, create an array of indexes sorted according to one of the arrays, then reorder the arrays according to the array of indexes.

const array1 = [200, 455.23, -306.5, 9000, -642.21, -133.9, 79.97, 1300];
const array2 = [
'2019-11-18T21:31:17.178Z',
'2019-12-23T07:42:02.383Z',
'2020-01-28T09:15:04.904Z',
'2020-04-01T10:17:24.185Z',
'2022-04-08T14:11:59.604Z',
'2022-04-10T17:01:17.194Z',
'2022-04-11T23:36:17.929Z',
'2022-04-13T10:51:36.790Z',
];

// create array of sorted indexes according to array1
var index = Array.from(Array(array1.length).keys())
          .sort((a, b) => array1[a] < array1[b] ? -1 : (array1[b] < array1[a]) | 0)

// reorder array1, array2, index in place according to index in O(n) time
var i;
var j;
var k;
var t1;
var t2;
for(i = 0; i < index.length; i++){
    if(i != index[i]){
        t1 = array1[i];
        t2 = array2[i];
        k = i;
        while(i != (j = index[k])){
            array1[k] = array1[j];
            array2[k] = array2[j];
            index[k]  = k;
            k = j;
        }
        array1[k] = t1;
        array2[k] = t2;
        index[k] = k;
    }
}
console.log(array1);
console.log(array2);

Related