Sort two arrayLists concurrently

前端 未结 4 1614
春和景丽
春和景丽 2021-01-07 11:38

Say I have two ArrayLists:

name: [Four, Three, One, Two]
num:  [4, 3, 1, 2]

If I do: Arrays.sort(num), then I have:

name: [         


        
4条回答
  •  不思量自难忘°
    2021-01-07 12:05

    Instead of sorting the actual arrays you can have an array with just indices

    a[i] = i for i = 0..n
    

    and you can sort this array based on your numeruc array with a custom comparator. e.g.

    bool compare( int a, int b ) { return num[a] < num[b]; }
    

    Thus you have both arrays sorted by using these indices.

提交回复
热议问题