简体   繁体   English

排序arrayLists

[英]sorting arrayLists

I have a scenario where I have two ArrayLists 我有一个场景,其中有两个ArrayList

ArrayList<String> sortedArrayList
ArrayList<String> unSortedArrayList

I have to sort unSortedArrayList depending on the sortedArrayList. 我必须根据sortedArrayList对unSortedArrayList进行排序。

ie, sortedArrayList is already sorted, now based on sortedArrayList, I have to sort unSortedArrayList. 即,sortedArrayList已经排序,现在基于sortedArrayList,我必须对unSortedArrayList进行排序。

unSortedArrayList size is <= to the size of sortedArrayList.

Is there a Java API for that? 是否有Java API?

Any help is appreciated. 任何帮助表示赞赏。

Using Google Guava 's excellent Ordering class: 使用Google Guava的出色Ordering类:

Collections.sort(unSortedArrayList, Ordering.explicit(sortedArrayList));

EDIT You can also do 编辑你也可以

List<whatever> sortedList = Ordering.explicit(sortedArrayList).immutableSortedCopy(unsortedArrayList);

As I understand what you have is that each element in list 1 has a corresponding element in list 2, and you want list 2 sorted into the order of the 'corresponding' elements. 据我了解,列表1中的每个元素在列表2中都有一个对应的元素,并且您希望列表2按“对应”元素的顺序排序。 Your best approach is to create an object to contain both Strings: 最好的方法是创建一个同时包含两个字符串的对象:

class StringPair {
  String s1;
  String s2;
}

Now make an array list of StringPairs and sort it based on the value of s1. 现在创建一个StringPairs数组列表,并根据s1的值对其进行排序。

        List<String> newSortedList = new ArrayList<String>();

        for(String currentSortedStr:sortedList){

            if(unsortedList.size==0)break;

            if(unsortedList.remove(currentSortedStr)){
                newSortedList.add(currentSortedStr);
            }
        }

You can do something like this if you mean what @Sam Dufel says in the comment 如果您的意思是@Sam Dufel在评论中所说的话,您可以执行以下操作

As far as I know there is not such API method for this case. 据我所知,这种情况下没有这种API方法。

This is not gonna take care of duplicates. 这将不会重复。 remove will remove only the first occurence of that object. remove将仅删除该对象的第一次出现。 At the if the unsorted list size is greater than 0, you can say it contains duplicates. 如果未排序的列表大小大于0,则可以说它包含重复项。 And if you need duplicates as well, you may wanna add some code to handle that case as well. 而且,如果您还需要重复项,则可能还需要添加一些代码来处理这种情况。

or if you mean the normal sorting; 或者,如果您的意思是正常排序;

Collections.sort(List<T>) will do the sorting for you. Collections.sort(List<T>)将为您进行排序。

Another way of doing it; 另一种方式;

Collections.sort(unsortedList,new CustomComparator(sortedList));

public class CustomComparator implements Comparator<String>{
        private List<String> sortedList;
        public CustomComparator(List<String> sortedList){
            this.sortedList = sortedList;
        }

        @Override
        public int compare(String o1, String o2) {
            return sortedList.indexOf(o1)-sortedList.indexOf(o2);
        }       
    }

Although your question is not clear enough I think that the following will help you. 尽管您的问题还不够清楚,但我认为以下内容将为您提供帮助。

You can use Collections.sort() to sort list. 您可以使用Collections.sort()对列表进行排序。 If you need some custom modification to sort mechanism implement your own Comparator and use 2 args version of this method: Collections.sort(list, comparable) 如果需要一些自定义修改来排序机制来实现自己的Comparator并使用此方法的2个args版本: Collections.sort(list, comparable)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM