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

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

问题描述

我无法找出根据一个列表的排序顺序对多个列表进行排序的最佳方法.目前,这些列表是根据它们的索引排序的.出发时间列表以 (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>();

我需要根据离开时间数组列表中出发时间的排序顺序对所有列表进行排序.在不更改结果数据结构的情况下对这些列表进行排序的最佳方法是什么?任何帮助将不胜感激.

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天全站免登陆