Sorting TreeSet using Comparator


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Sorting TreeSet using Comparator

  1. #1
    Join Date
    Aug 2005
    Posts
    8

    Unhappy 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

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    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

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

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
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

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