A curious case of sorting two arrays in tandem

Suppose we have two arrays:

a = [4,3,8,7]
b = [(1,2),(5,6),(8,6),(9,0)] 

So what we want is to sort the array a. So the sort result should be a_sorted = [3,4,7,8]. And we should not sort array b. Instead, the order of array b should be changed according to the sort order of array a.

So array b should be b_sorted = [(5,6),(1,2),(9,0),(8,6)]

ie, the order of a_sorted will be a_sorted = [a[1],a[0],a[3],a[2]]. Respectively,b_sorted = [b[1],b[0],b[3],b[2]]

The question is simpler. Is there a name for this sort of sorting ?:

+3
source share
5 answers

, , , . , , . , :

  • , , .

  • . .

, , , , , - - 4 8 .

API . , .NET Array (, ), , .

+2

([2,1,4,3] a) . .

, Matlab [sortedA, sortedBy] = sort([4 3 8 7]); Then sortedA = a(sortedBy) = [3 4 7 8] sortedBy = [2 1 4 3], b b(sortBy).

+3

, . , " " (), , ( ). , - , .

It all depends on the situation, of course. You can use a language that does not have the ability to group related attributes in (user) objects.

+2
source

Add values bto the keys ato get a multidimensional array. Then sort this array.

+1
source

Yes, in PHP there is an array sort function called array_multisort that does what you want.

0
source

All Articles