java中Arraylist<String>的ArrayList排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16184653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sorting ArrayList of Arraylist<String> in java
提问by Nikhil Agrawal
I have an ArrayList of ArrayList of String.
我有一个字符串的 ArrayList 的 ArrayList。
In Outer ArrayList on each index each Inner ArrayList has four items have four parameters.
在 Outer ArrayList 上的每个索引上,每个 Inner ArrayList 有四个项目有四个参数。
- Contacts Id
- Contacts Name
- Contacts Adress
- Contacts Number
- 联系人 ID
- 联系人姓名
- 联系人地址
- 联系人号码
Now I want to sort the complete ArrayList of the on the basis of Contact Name Parameter.
现在我想根据联系人姓名参数对完整的 ArrayList 进行排序。
Means I want to access the outer Arraylist and the inner ArrayList present on each index of outer Arraylist should be sorted according to contact Name.
意味着我想访问外部 Arraylist 和外部 Arraylist 的每个索引上存在的内部 ArrayList 应根据联系人姓名进行排序。
Comparator / Comparable Interfaces not likely to help me.
Comparator / Comparable Interfaces 不太可能帮助我。
Collection.sort can't help me
Collection.sort 帮不了我
Sorting Arraylist of Arraylist of Bean. I have read this post but it is for ArrayList
of ArrayList<Object>
. How to figure out this problem?
对 Bean 的 Arraylist 的 Arraylist 进行排序。我已阅读这篇文章,但它是对ArrayList
的ArrayList<Object>
。如何弄清楚这个问题?
采纳答案by Bohemian
Assuming your Lists in your List has Strings in the order id, name, address and number (i.e. name is at index 1), you can use a Comparator
, as follows:
假设您的 List 中的 Lists 具有按 id、name、address 和 number 顺序排列的字符串(即 name 位于索引 1),您可以使用 a Comparator
,如下所示:
List<List<String>> list;
Collections.sort(list, new Comparator<List<String>> () {
@Override
public int compare(List<String> a, List<String> b) {
return a.get(1).compareTo(b.get(1));
}
});
Incidentally, it matters not that you are using ArrayList
: It is good programming practice to declare variables using the abstract type, i.e. List
(as I have in this code).
顺便说一句,这与您是否使用无关ArrayList
:使用抽象类型(即List
(正如我在此代码中所做的那样))声明变量是一种很好的编程习惯。
回答by Stephen C
Comparator / Comparable Interfaces can't help because I don't have any objects.
Comparator / Comparable Interfaces 无能为力,因为我没有任何对象。
Incorrect. You do have objects. All of the things you are trying to sort are objects.
不正确。你确实有对象。您尝试排序的所有事物都是对象。
If you are trying to sort the ArrayList<String>
objects in an ArrayList<ArrayList<String>>
, you need to implement a Comparator<ArrayList<String>>
. (The Comparable approach is the wrong one for this data structure. You would need to declare a custom subclass of ArrayList ... and that's yuck!)
如果您尝试对 中的ArrayList<String>
对象进行排序ArrayList<ArrayList<String>>
,则需要实现Comparator<ArrayList<String>>
. (Comparable 方法对于这个数据结构来说是错误的。你需要声明一个 ArrayList 的自定义子类......这太糟糕了!)
But a better idea would be to represent your objects with custom classes. In this case, your ArrayList of String
should be a custom Contact
class with 4 fields, getters and (if required) setters. Then you declare that as implementing Comparable<Contact>
, and implement the compareTo
method.
但是更好的主意是用自定义类来表示您的对象。在这种情况下,您ArrayList of String
应该是一个Contact
具有 4 个字段、getter 和(如果需要)setter的自定义类。然后您将其声明为 implementation Comparable<Contact>
,并实现该compareTo
方法。
Other Answers show how to implement a Comparator based on just one field of the list. That may be sufficient, but it will give you a sort order where the order of a pair of different "John Smith"s would be indeterminate. (I would use a second field as a tie-breaker. The Id field would be ideal if the ids are unique.)
其他答案显示了如何仅基于列表的一个字段来实现比较器。这可能就足够了,但它会给你一个排序顺序,其中一对不同的“John Smith”的顺序是不确定的。(我会使用第二个字段作为决胜局。如果 id 是唯一的,那么 Id 字段将是理想的。)
回答by aquaraga
I think this is a case of not treating collections as first-class objects. Have a new class called "Contact" instead of abstracting it as an ArrayList, and use the Comparator.
我认为这是不将集合视为一流对象的情况。有一个名为“Contact”的新类,而不是将其抽象为一个 ArrayList,并使用 Comparator。
回答by NilsH
You should create a class for your data structure (if you can't, then specify why. I can't see any good reason not to):
您应该为您的数据结构创建一个类(如果不能,请说明原因。我看不出有什么好的理由不这样做):
public class Contact implements Comparable {
private String id;
private String name;
private String address;
private String number;
// Getters and setters, and compareTo.
}
Then use that in your list instead:
然后在您的列表中使用它:
List<Contact> contacts = new ArrayList<Contacts>();
Sorting it will then be trivial.
对它进行排序将是微不足道的。
回答by jlordo
I feel bad posting this, because List<Contact>
would be the much betterchoice. Something like this would be possible, though:
我感觉不好张贴这个,因为List<Contact>
会是更好的选择。不过,这样的事情是可能的:
ArrayList<ArrayList<String>> yourList = ...
Collections.sort(yourList, new Comparator<ArrayList<String>>() {
@Override
public int compare(ArrayList<String> one, ArrayList<String> two) {
return one.get(1).compareTo(two.get(1));
}
});
回答by Arjun Rao
Use the following Comparator:
使用以下比较器:
class MyComparator implements Comparator<ArrayList<String>> {
private static int indexToCompare = 1;
@Override
public int compare(ArrayList<String> o1, ArrayList<String> o2) {
return o1.get(indexToCompare).compareTo(o2.get(indexToCompare));
}
}
Here indexToCompare
is the index of the arraylist which corresponds to Contact Name. In your case "1"
这indexToCompare
是对应于联系人姓名的数组列表的索引。在你的情况下“1”
回答by msoori
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class ListsUtils {
public static void sortListOfLists(List < List < String >> listOfLists) {
// first sort the inner arrays using collections.sort
for (List < String > innerList: listOfLists) {
Collections.sort(innerList);
}
// now sort by comparing the first string of each inner list using a comparator
Collections.sort(listOfLists, new ListOfStringsComparator());
}
static final class ListOfStringsComparator implements Comparator < List < String >> {
@
Override
public int compare(List < String > o1, List < String > o2) {
// do other error checks here as well... such as null. outofbounds, etc
return o1.get(0).compareTo(o2.get(0));
}
}
}
I guess I just assumed you had to sort a list of string arrays... thats why I sorted the list of inner arrays first, then sorted the outer list by comparing the 1st item of each array. Didnt read the contacts you had in your answer.
我想我只是假设您必须对字符串数组列表进行排序……这就是为什么我首先对内部数组列表进行排序,然后通过比较每个数组的第一个项目对外部列表进行排序。没有阅读您在回答中的联系方式。
In that case remove the for loop for sorting the inner list and you should still be able to sort using the comparator, but compare to the right index instead of the 1st element.
在这种情况下,删除用于对内部列表进行排序的 for 循环,您仍然应该能够使用比较器进行排序,但与正确的索引而不是第一个元素进行比较。
Collections.sort(listOfLists, new ListOfStringListComparator());
Collections.sort(listOfLists, new ListOfStringListComparator());