Sort two arrayLists concurrently

前端 未结 4 1611
春和景丽
春和景丽 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:12

    In some cases it doesn't make much sense to create a new class just to do multiple sorts based off a given list. I've created a function that does this, but I've posted the code in a another SO post so I wont repeat it. Below is an example of how to use it.


    Usage

    Here is an example of how you can use the function to sort multiple lists of arbitrary types:

    // The key can be any type that implements Comparable, Dupes are allowed
    List key = Arrays.asList(4, 3, 1, 2, 1);
    
    // List Types do not need to be the same
    List list1 = Arrays.asList("Four", "Three", "One", "Two", "One");
    List list2 = Arrays.asList('d', 'c', 'a', 'b', 'a');
    
    // Sorts key, list1, list2 using key as the sorting key.
    keySort(key, key, list1, list2);
    

    Output:

    key:   [1, 1, 2, 3, 4]
    list1: [One, One, Two, Three, Four]
    list2: [a, a, b, c, d]
    

提交回复
热议问题