java中如何对多个数组进行排序 [英] How to sort multiple arrays in java

查看:27
本文介绍了java中如何对多个数组进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试按字典顺序对三个数组进行排序.这些数组通过一个公共数组相互关联.如果我演示一下就更容易解释了:

I'm trying to sort three arrays by lexicographical order. The arrays are related to each other by a common array. It's easier to explain if I demonstrate:

int[] record = new int[4];
String [] colors = {"blue", "yellow", "red", "black"};
String [] clothes = {"shoes", "pants", "boots", "coat"};

在控制台上打印时,我希望将它们放在类似于下面的三列中:

When printed on the console, I would like them to be put in three columns similar to below:

Record  Color   Clothes
0       blue    shoes
1       yellow  pants
2       red     boots
3       black   coat

按颜色排序:

Record  Color   Clothes
3       black   coat
0       blue    shoes
2       red     boots
1       yellow  pants

按衣服排序:

Record  Color   Clothes
2       red     boots
3       black   coat
1       yellow  pants
0       blue    shoes

我找到了一个与我的场景类似的先前答案,但它比较的是整数而不是字符串,而且我在使用 compareTo() 方法和 Arrays.sort() 到达我想要的输出.

I found a previous answer similar to my scenario, but it compared integers instead of strings, and I'm having trouble using the compareTo() method and Arrays.sort() to arrive at my desired output.

任何帮助将不胜感激!

推荐答案

在某些情况下,创建一个新类只是为了进行排序并没有多大意义.

In some cases it doesn't make much sense to create a new class just to do sorting.

这里是一个函数,可用于根据键列表(List)对任意数量的任意类型列表(List)进行排序.).此处为 Ideone 示例.

Here, is a function that can be used to sort an any number of arbitrarily typed lists (List<?>) based on a key list (List<T implements Comparable>). Ideone Example here.

以下示例说明如何使用该函数对多个任意类型的列表进行排序:

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

List<Integer> ids = Arrays.asList(0, 1, 2, 3);
List<String> colors = Arrays.asList("blue", "yellow", "red", "black");
List<String> clothes = Arrays.asList("shoes", "pants", "boots", "coat");

// Sort By ID
concurrentSort(ids, ids, colors, clothes);

// Sort By Color
concurrentSort(colors, ids, colors, clothes);

// Sort By Clothes
concurrentSort(clothes, ids, colors, clothes);

输出:

// Sorted By ID:
ID:      [0, 1, 2, 3]
Colors:  [blue, yellow, red, black]
Clothes: [shoes, pants, boots, coat]

// Sorted By Color:
ID:      [3, 0, 2, 1]
Colors:  [black, blue, red, yellow]
Clothes: [coat, shoes, boots, pants]

// Sorted By Clothes:
ID:      [2, 3, 1, 0]
Colors:  [red, black, yellow, blue]
Clothes: [boots, coat, pants, shoes]

<小时>

代码

可以在此处找到 Ideone 示例,其中包括参数验证和测试案例.


Code

An Ideone Example can be found here which includes validation of parameters and a test case.

public static <T extends Comparable<T>> void concurrentSort(
                                        final List<T> key, List<?>... lists){
    // Create a List of indices
    List<Integer> indices = new ArrayList<Integer>();
    for(int i = 0; i < key.size(); i++)
        indices.add(i);

    // Sort the indices list based on the key
    Collections.sort(indices, new Comparator<Integer>(){
        @Override public int compare(Integer i, Integer j) {
            return key.get(i).compareTo(key.get(j));
        }
    });

    // Create a mapping that allows sorting of the List by N swaps.
    // Only swaps can be used since we do not know the type of the lists
    Map<Integer,Integer> swapMap = new HashMap<Integer, Integer>(indices.size());
    List<Integer> swapFrom = new ArrayList<Integer>(indices.size()),
                  swapTo   = new ArrayList<Integer>(indices.size());
    for(int i = 0; i < key.size(); i++){
        int k = indices.get(i);
        while(i != k && swapMap.containsKey(k))
            k = swapMap.get(k);

        swapFrom.add(i);
        swapTo.add(k);
        swapMap.put(i, k);
    }

    // use the swap order to sort each list by swapping elements
    for(List<?> list : lists)
        for(int i = 0; i < list.size(); i++)
            Collections.swap(list, swapFrom.get(i), swapTo.get(i));
}

<小时>

注意:运行时间为O(mlog(m) + mN),其中m是列表的长度,N 是列表的数量.通常 m >>N 所以运行时间并不比只排序 key O(mlog(m)) 重要.


NOTE: The running time is O(mlog(m) + mN) where m is the length of the list and N is the number of lists. Usually m >> N so the running time is not more significant than sorting only the key O(mlog(m)).

这篇关于java中如何对多个数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆