Java 如何仅按第一个元素对 2D ArrayList<String> 进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20480723/
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
How to Sort 2D ArrayList<String> by Only the First Element
提问by Michael Yaworski
To avoid the duplicate claims, I've looked at thispost and it isn't exactly what I wanted.
Every other 2D ArrayList question involved double
or int
numbers; my question is about Strings
.
为了避免重复声明,我查看了这篇文章,但它并不是我想要的。
涉及的所有其他 2D ArrayList 问题double
或int
数字;我的问题是关于Strings
.
What I'm doing
我在做什么
I have a 2D ArrayList, defined like this:
我有一个 2D ArrayList,定义如下:
ArrayList<ArrayList<String>> namesAndNumbers = new ArrayList<ArrayList<String>>();
The idea is that the first item in every row of ArrayLists contains the names, and the rest of the columns in each row contains the phone numbers (unknown amount). Therefore I would like to avoid converting it to a regular array.
这个想法是 ArrayLists 每一行中的第一项包含名称,每行中的其余列包含电话号码(未知数量)。因此我想避免将其转换为常规数组。
Sample
样本
Let's say I've populated my ArrayList and I have this:
假设我已经填充了我的 ArrayList 并且我有这个:
{"Mike", "(805) 766-4920"}
{"Emily", "(705) 668-9292", "(705) 555-1060"}
{"James", "(605) 965-2000"}
I expect my outputto be this:
我希望我的输出是这样的:
{"Emily", "(705) 668-9292", "(705) 555-1060"}
{"James", "(605) 965-2000"}
{"Mike", "(805) 766-4920"}
I want to keep the numbers corresponding with the names, but simply sort the array by the name.
我想保留与名称对应的数字,但只需按名称对数组进行排序。
What answers I hope for
我希望得到什么答案
I'm hoping for a built-in function manipulation type of thing, but I'd be fine if someone created a sorting array method for me with an 2D ArrayList as the input. I haven't seen any question that answers this issue explicitly. I will continue trying to come up with an answer myself as well.
我希望有一种内置的函数操作类型,但如果有人为我创建了一个以 2D ArrayList 作为输入的排序数组方法,我会很好。我还没有看到任何明确回答这个问题的问题。我自己也会继续努力想出一个答案。
采纳答案by user2336315
You can use Collections.sort
and provide a custom Comparator
where you compare the first element of each list, i.e the name :
您可以使用Collections.sort
并提供一个自定义来Comparator
比较每个列表的第一个元素,即名称:
List<ArrayList<String>> namesAndNumbers = new ArrayList<ArrayList<String>>();
namesAndNumbers.add(new ArrayList<String>(Arrays.asList("Mike", "(805) 766-4920")));
namesAndNumbers.add(new ArrayList<String>(Arrays.asList("Emily", "(705) 668-9292", "(705) 555-1060")));
namesAndNumbers.add(new ArrayList<String>(Arrays.asList("James", "(605) 965-2000")));
Collections.sort(namesAndNumbers, new Comparator<ArrayList<String>>() {
@Override
public int compare(ArrayList<String> o1, ArrayList<String> o2) {
return o1.get(0).compareTo(o2.get(0));
}
});
System.out.println(namesAndNumbers);
Output :
输出 :
[[Emily, (705) 668-9292, (705) 555-1060], [James, (605) 965-2000], [Mike, (805) 766-4920]]
回答by Yehia Awad
For diversity Why dont you just use a HashMap unless you want duplicate keys. the key would be name and the value would be an ArrayList of numbers
为了多样性为什么不只使用 HashMap 除非你想要重复的键。键是名称,值是数字的 ArrayList
String name="John";
ArrayList<String> phonenumbers=new ArrayList<String>();
phonenumbers.add("80925887");
phonenumbers.add("+166476654");
TreeMap<String, ArrayList> tm=new TreeMap<String, ArrayList>();
tm.put(name, phonenumbers);
回答by Hollis Waite
Create a custom comparator:
创建自定义比较器:
final Comparator<List<String>> comparator = new Comparator<List<String>>() {
public int compare(List<String> pList1, List<String> pList2) {
return pList1.get(0).compareTo(pList2.get(0));
}
};
final List<List<String>> lists = Arrays.asList(
Arrays.asList("Mike", "(805) 766-4920"),
Arrays.asList("Emily", "(705) 668-9292", "(705) 555-1060"),
Arrays.asList("James", "(605) 965-2000")
);
Collections.sort(lists, comparator);
for (List<String> list : lists) System.out.println(list);