I have the elements in Arraylist like
"Sowmya/20开发者_如何学编程"
"Ramya/67"
"Archana/54"
"Kavitha/48"
.
.
.
Now I have to arrange these elements in descending order(by numbers) like
"Ramya/67"
"Archan/54"
"kavitha/48"
"Sowmya/20"
How can I do this?
This should do the trick :
Collections.sort(myList, new Comparator<String>() {
public int compare (String s1, String s2) {
int i1 = Integer.parseInt(s1.split("/")[1]);
int i2 = Integer.parseInt(s2.split("/")[1]);
return i1 - i2;
}
});
Of course you will need to add some checks for production code (is there a slash in strings, is the second part always an integer, etc...)
精彩评论