排序列表<列表<字符串>>按清单值 [英] Sort List<List<String>> by list value

查看:104
本文介绍了排序列表<列表<字符串>>按清单值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想按列表中的值对列表进行排序,它包含三个值,第一个值是整数,我将其转换为字符串,其他两个值本质上是字符串,我想按第一个字符串对列表进行排序.

I want sort the List by values in the list , it contains three values , first value is integer i convert that into string and other two values are string in nature, i want to sort the list by first string .

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

List<String> fieldValues =fieldValues = new ArrayList<String>();
String aString = Integer.toString(ScreenId);
fieldValues.add(aString);
fieldValues.add(DisplayScreenName);
fieldValues.add(TableDisplay);

detail_View.add(fieldValues);

在上面的代码中,我必须按ScreenId对列表值进行排序

In above code i have to sort list values by ScreenId

推荐答案

您必须使用两个概念:

  1. Collections.sort实用程序.
  2. Comparator<T>界面.
  1. Collections.sort utility.
  2. Comparator<T> interface.

我写了以下您解决的问题:

I write your solved problem following:

首先,您必须编写比较器:

First you have to write your comparator:

class CustomComparator implements Comparator<List<String>>
    {
        @Override
        public int compare(List<String> o1,
            List<String> o2)
        {
            String firstString_o1 = o1.get(0);
            String firstString_o2 = o2.get(0);
            return firstString_o1.compareTo(firstString_o2); 
        }
    }

然后您使用Collections实用程序,如下所示:

then you using Collections utility as following:

Collections.sort(detail_View, new CustomComparator());

完成这些步骤后,您的列表:

after these step, your list:

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

将按任何嵌套列表的第一个索引排序.

will sorted by first index of any nested list.

有关此概念的更多信息,请参见:

For more information related to this concept see:

  1. http://www.javadeveloper.co.in/java-example/java-comparator-example.html
  2. http://www.roseindia.net/java/java-tips/data/collections_non_generic/comparators.shtml
  1. http://www.javadeveloper.co.in/java-example/java-comparator-example.html
  2. http://www.roseindia.net/java/java-tips/data/collections_non_generic/comparators.shtml

这篇关于排序列表&lt;列表&lt;字符串&gt;&gt;按清单值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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