Reputation: 2353
Quick question, just say I have a list/collection of nodes made up of hostnames or IP's. I now want to sort them alphabetically then numerically so thinking using .sort() call is right way to go?
Upvotes: 2
Views: 992
Reputation: 29473
If nodes implement java.lang.Comparable
, you can define custom ordering. If you can not change/modify the node class, then you can implement java.util.Comparator
, and use Collection.sort(List<T> list, Comparator<? super T> c)
Effective Java from Bloch has excellent section on this topic.
Upvotes: 2
Reputation: 240880
You can create a class
class Foo {
int no;
String val;
}
and
Collections.sort(listOfFoo,new Comparator<Foo>(){public int compare(Foo f1, Foo f2){
//your logic goes here
}});
Upvotes: 0