是否可以在忽略每个字符串中的前 3 个字符的同时对数组列表进行排序? [英] Is it possible to sort a an array list while ignoring the first 3 characters in each string?
本文介绍了是否可以在忽略每个字符串中的前 3 个字符的同时对数组列表进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想弄清楚如何按字母顺序对我的列表进行排序,通常这很容易,但我需要忽略列表中每个字符串的前 5 个字符.(它们是数字IDS)
I am trying to figure out how to sort my list alphabetically, normally this would be really easy, but I need to ignore the first 5 characters of each string in my list. (they are numerical IDS)
ArrayList<String> tempList = new ArrayList<String>();
for(String s : AddressBook){
tempList.add(s);
Collections.sort(tempList );
}
System.out.println(tempList);
推荐答案
您可以通过提供自己的 Comparator 实现来实现.
You can do it by supplying your own Comparator implementation.
Collections.sort (tempList, new Comparator<String>() {
public int compare(String o1, String o2)
{
String sub1 = o1.substring (3);
String sub2 = o2.substring (3);
return sub1.compareTo (sub2);
}
});
这篇关于是否可以在忽略每个字符串中的前 3 个字符的同时对数组列表进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文