Arraylist<String>的排序ArrayList在 Java 中 [英] Sorting ArrayList of Arraylist<String> in java
问题描述
我有一个字符串的 ArrayList 的 ArrayList.
I have an ArrayList of ArrayList of String.
In Outer ArrayList 每个索引上的每个 Inner ArrayList 有四个项目有四个参数.
In Outer ArrayList on each index each Inner ArrayList has four items have four parameters.
- 联系人 ID
- 联系人姓名
- 联系地址
- 联系电话
现在我想根据联系人姓名参数对完整的 ArrayList 进行排序.
Now I want to sort the complete ArrayList of the on the basis of Contact Name Parameter.
意思是我想访问外层 Arraylist 和外层 Arraylist 的每个索引上存在的内层 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.
Comparator/Comparable Interfaces 不太可能帮到我.
Comparator / Comparable Interfaces not likely to help me.
Collection.sort 帮不了我
Collection.sort can't help me
对Bean的Arraylist的Arraylist进行排序.我已经阅读了这篇文章,但它是针对 ArrayList
的 ArrayList
的.如何解决这个问题?
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?
推荐答案
假设你的 List 中的 Lists 在 id、name、address 和 number 的顺序中有 Strings(即 name 在索引 1),你可以使用 Comparator
,如下:
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<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));
}
});
顺便说一句,您是否使用 ArrayList
并不重要:使用抽象类型声明变量是一种很好的编程习惯,即 List
(就像我在这段代码中所做的那样)).
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<String>的排序ArrayList在 Java 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!