排序的ArrayList&LT的ArrayList中,串>在java中 [英] Sorting ArrayList of Arraylist<String> in java

查看:140
本文介绍了排序的ArrayList&LT的ArrayList中,串>在java中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有字符串ArrayList的一个ArrayList

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.


  1. 联系人标识

  2. 联系人姓名

  3. 联系人ADRESS

  4. 通讯录号

现在我要的完整的ArrayList排序联系人名称参数的基础上。

Now I want to sort the complete ArrayList of the on the basis of Contact Name Parameter.

意味着我要访问的外部ArrayList和在外ArrayList的每个索引内的ArrayList present应根据接触名称进行排序。

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 not likely to help me.

Col​​lection.sort不能帮我。

Collection.sort can't help me

排序豆的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?

推荐答案

在您的列表假设你列出有顺序编号,姓名,地址和电话号码字符串(即名称为索引1),你可以使用比较,如下:

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 :这是很好的编程习惯使用抽象类型,即声明变量列表(因为我有这个code)。

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&LT的ArrayList中,串&GT;在java中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆