我有一个String的arrayList,其中包含以下字符串
Common Ruby Errors 45min
Rails for Python Developers lightning
Communicating Over Distance 60min
Accounting-Driven Development 45min
Woah 30min
Sit Down and Write 30min
现在我想根据每个字符串中提到的数字按降序对arraylist进行排序
即。排序列表应该看起来像
Communicating Over Distance 60min
Common Ruby Errors 45min
Accounting-Driven Development 45min
Woah 30min
Sit Down and Write 30min
Rails for Python Developers lightning
任何人都可以告诉我该怎么做?
答案 0 :(得分:3)
正如@Rohit建议的那样,你的任务是双重的
Comparator
并执行sortedList = Collections.sort( list , new YourComparator() )
这样的事情可能是可行的:
Comparator c = new Comparator<String>()
{
@Override
public int compare( String o1, String o2 )
{
return getNumber( o1 ).compareTo( getNumber( o2 ) );
}
private Integer getNumber( String str )
{
Pattern pattern = Pattern.compile("([0-9]+)");
Matcher matcher = pattern.matcher(str);
while ( matcher.find() ) { return Integer.parseInt( matcher.group( 1 ) ); }
return 0;
}
}
干杯,
答案 1 :(得分:1)
如果你有那些你只需要打电话
Collections.sort(yourLIstWithString, new YourNumberInStringComparator());