排序列表<列表<字符串>>按清单值 [英] Sort List<List<String>> by list value
问题描述
我想按列表中的值对列表进行排序,它包含三个值,第一个值是整数,我将其转换为字符串,其他两个值本质上是字符串,我想按第一个字符串对列表进行排序.
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
推荐答案
您必须使用两个概念:
-
Collections.sort
实用程序. -
Comparator<T>
界面.
Collections.sort
utility.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:
- http://www.javadeveloper.co.in/java-example/java-comparator-example.html
- http://www.roseindia.net/java/java-tips/data/collections_non_generic/comparators.shtml
- http://www.javadeveloper.co.in/java-example/java-comparator-example.html
- http://www.roseindia.net/java/java-tips/data/collections_non_generic/comparators.shtml
这篇关于排序列表<列表<字符串>>按清单值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!