javascript, sort 2 array dependently

后端 未结 6 615
渐次进展
渐次进展 2021-01-04 10:09

for hours i\'ve been trying to figure out how to sort 2 array dependently.

Let\'s say I have 2 arrays.

First one:

array1 = [\'zzzzz\', \'aaaa         


        
6条回答
  •  一整个雨季
    2021-01-04 10:24

    I would "zip" them into one array of objects, then sort that with a custom sort callback, then "unzip" them back into the two arrays you wanted:

    var array1 = ['zzzzz', 'aaaaaa', 'ccccc'],
        array2 = [3, 7, 1],
        zipped = [],
        i;
    
    for(i=0; i

    Here's a working fiddle.

提交回复
热议问题