daverocks
daverocks

Reputation: 2353

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 .sort() call is right way to go?

Upvotes: 2

Views: 992

Answers (3)

Op De Cirkel
Op De Cirkel

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

Jigar Joshi
Jigar Joshi

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

amit
amit

Reputation: 178421

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

Upvotes: 1

Related Questions