-
Sorting TreeSet using Comparator
hello
i'll tell u what ive been doing so far..
Ive got 3 classes,
1) Person (interface extends Comparable)
2) ThePerson (implements Person)
3) PersonList (TreeSet holds collection of Person class)
the Persons will be sorted based on their ID numbers.. so this should be done automatically because im using TreeSet, is this correct?
Now, i want the collection to be slso sorted using another attribue that is "name", iv got a comparator ready to do that, but i cant use
collections.sort(treeSet, comp) <-- because its a Set!!
keeping in mind that i want to be able to sort the records in either ID or name...
if u think its not possible then please say so, i dont want to spend more time on something that cant be solved
can some1 please please help me out
Hannah
-
Many ways to skin the cat ?
The TreeSet will gurantee a sort in the natural order of the elements, as I
understand that, it means that Strings will be sorted lexically ascending, and numerics
will be sorted on ascending numerical order. If the TheeSet contains custom objects
I cannot see any other sort order than their reference (address). The TreeSet's
comparator must be included in the constructor so you are stuck with one sort method
from the start.
If you just need sorting and an ArrayList will do the rest of the job for you,
you could consider the following approach:
Code:
import java.util.*;
class Person {
private int id=-1;
private String name=null;
public Person (int id, String name) {
this.id=id;
this.name=name;
}
public int getId() { return id; }
public String getName() { return name; }
public String toString () {
return "ID:"+id+", Name:"+name;
}
}
class MyCompare implements Comparator {
public final static int BY_ID=0;
public final static int BY_NAME=1;
private int sortType=BY_ID;
public void setSortType(int sortType) {
this.sortType=sortType;
}
public int compare(Object o1, Object o2) {
Person p1=(Person)o1;
Person p2=(Person)o2;
switch (sortType) {
case BY_ID:
return p1.getId()-p2.getId();
case BY_NAME:
return p1.getName().compareTo(p2.getName());
default:
return 0; // huh ?
}
}
public boolean equals(Object obj) { // not used by the sort
return false;
}
}
public class Sorter {
public Sorter() {
MyCompare mc=new MyCompare();
ArrayList list=new ArrayList();
list.add(new Person(1,"Beta"));
list.add(new Person(2,"Alpha"));
Collections.sort(list, mc);
System.out.println(list);
mc.setSortType(MyCompare.BY_NAME);
Collections.sort(list, mc);
System.out.println(list);
}
public static void main(String[] args) {
Sorter tst = new Sorter();
}
}
eschew obfuscation
-
aha, thank u very very much
hannah
Similar Threads
-
Replies: 3
Last Post: 07-29-2005, 10:00 AM
-
By tomorrowland in forum Java
Replies: 1
Last Post: 10-07-2002, 11:14 PM
-
By Basel in forum ASP.NET
Replies: 1
Last Post: 04-05-2002, 10:18 AM
-
By Ramkumar in forum Java
Replies: 1
Last Post: 01-11-2001, 05:32 AM
-
By Joel Matto in forum Web
Replies: 0
Last Post: 05-02-2000, 03:58 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks