DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2005

    Unhappy Sorting TreeSet using Comparator


    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

  2. #2
    Join Date
    Nov 2004

    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:
    import java.util.*;
    class Person  {
      private int id=-1;
      private String name=null;
      public Person (int id, String 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) {
      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());
            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);
        Collections.sort(list, mc);
      public static void main(String[] args) {
        Sorter tst = new Sorter();
    eschew obfuscation

  3. #3
    Join Date
    Aug 2005
    aha, thank u very very much

Similar Threads

  1. Replies: 3
    Last Post: 07-29-2005, 10:00 AM
  2. The need to implement equals for a TreeSet
    By tomorrowland in forum Java
    Replies: 1
    Last Post: 10-07-2002, 11:14 PM
  3. Replies: 1
    Last Post: 04-05-2002, 10:18 AM
  4. Sorting for a JTable
    By Ramkumar in forum Java
    Replies: 1
    Last Post: 01-11-2001, 05:32 AM
  5. CrossBrowser DHTML table sorting problem
    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
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.