开发者

Sorting elements alphabetically then numerically

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 .so开发者_高级运维rt() call is right way to go?


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.


just implement a Comperator and use Arrays.sort() as you assumed


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
}});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜