如何根据另一个ArrayLists的顺序对多个ArrayLists进行排序? [英] How to sort multiple ArrayLists based off order of another?

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

问题描述

我无法找出基于一个列表的排序顺序排序多个列表的最佳方法。目前,这些清单是根据其指数排序的。 departureTime列表以格式(00:00 AM / PM)保存时间串。它们初始化如下:

I'm having trouble figuring out the best way to go out about sorting multiple lists based off of the sorted order of one list. Currently the lists are in order based off their indices. The departureTime list holds strings of time in the format (00:00 AM/PM). They are initialized like this:

public static List<String> departureTime = new ArrayList<String>();
public static List<String> mode = new ArrayList<String>(); 
public static List<String> busNo = new ArrayList<String>();
public static List<String> busStopName = new ArrayList<String>();
public static List<String> arrivalTime = new ArrayList<String>();
public static List<String> dur = new ArrayList<String>();

我需要根据departureTime ArrayList中出发时间的排序顺序对所有列表进行排序。在不改变结果数据结构的情况下,对这些列表进行排序的最佳方法是什么。任何帮助都将受到高度赞赏。

I need to sort all lists based of the sorted order of departure times in the departureTime ArrayList. What's the best way to go about sorting these lists without changing the resulting data structure. Any assistance would be highly appreciated.

谢谢,

马特

推荐答案

如评论中所述,创建包含所有值的对象会更容易,然后只对对象列表进行排序。

As stated in the comment, it would have been easier to create an object containing all the values, and then just sort the object list.

如果出于某种原因这是不可能的,你需要编写自己的排序方法,例如选择排序,同时对列表中的所有对象进行排列操作。

If this is for some reason impossible, you will need to code your own sort method, for instance selection sort, while making the permutations operations on all objects in the lists.

这是一个简单但非最优的算法。随意适应其他类型。只有当所有列表具有相同的长度时,这才有效。

Here is a simple but non optimal algorithm. Feel free to adapt it for some other sort. This would work only if all lists have the same length.

    public void sort() {
    String[] departureTimeArray = departureTime.toArray(new String[departureTime.size()]);
    String[] modeArray = mode.toArray(new String[mode.size()]);
    //here you convert the other lists to arrays

    int lenD = departureTimeArray.length;
    int j = 0;
    for(int i=0;i<lenD;i++){
        j = i;
        for(int k = i;k<lenD;k++){
            if(departureTimeArray[j].compareTo(departureTimeArray[k])>0){
                j = k;
            }
        }
        permutation(departureTimeArray, i, j);
        permutation(modeArray, i, j);
        //here do the same for other arrays
    }

    departureTime = Arrays.asList(departureTimeArray);
    mode = Arrays.asList(modeArray);
    //here convert back arrays to list
}

private void permutation(String[] array, int i, int j) {
    String tmp = array[i];
    array[i] = array[j];
    array[j] = tmp;
}

例如,请参阅此处的选择排序算法:
选择排序

See for instance here for the selection sort algorithm : selection sort

这篇关于如何根据另一个ArrayLists的顺序对多个ArrayLists进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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