Women in Technology

Hear us Roar



Article:
  Making Java Objects Comparable
Subject:   Regarding sorting of objects
Date:   2006-05-10 22:19:48
From:   Quetion
HI,
This article is good..
But i am not getting how to sort the objects based on two conditons simultaneously.
For example if i want to sort the emp details based on first name and age. The condition is the first sort the objects inbetween age group 20-30 and within that group sort alphabetically.
please will you send a soulution for this..
waiting for your reply...


thank you



Regards
Anirudhkumar

Main Topics Oldest First

Showing messages 1 through 2 of 2.

  • Regarding sorting of objects
    2006-12-20 02:18:03  hirenaik [View]

    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Comparator;

    class Person{
    private String empName=null;
    private String empAdd=null;
    private int age=0;

    Person(String name, String add){
    empName=name;
    empAdd=add;
    }
    public String getEmpAdd() {
    return empAdd;
    }
    public String getEmpName() {
    return empName;
    }
    public void setEmpAdd(String string) {
    empAdd = string;
    }
    public void setEmpName(String string) {
    empName = string;
    }

    static Comparator sortByEmpName = new Comparator(){
    public int compare(Object person, Object anotherPerson) {
    String empName1 = ((Person) person).getEmpName();
    String empName2 = ((Person) anotherPerson).getEmpName();
    String empAdd1 = ((Person)person).getEmpAdd();
    String empAdd2 = ((Person)anotherPerson).getEmpAdd();
    System.out.println("sorting");
    if(!empName1.equals(empName2))
    return empName1.compareTo(empName2);
    else
    return empAdd1.compareTo(empAdd2);
    }
    };

    static Comparator sortByEmpAdd = new Comparator(){
    public int compare(Object person, Object anotherPerson) {
    String empName1 = ((Person) person).getEmpName();
    String empName2 = ((Person) anotherPerson).getEmpName();
    String empAdd1 = ((Person)person).getEmpAdd();
    String empAdd2 = ((Person)anotherPerson).getEmpAdd();
    System.out.println("sorting");
    if(!empAdd1.equals(empAdd2))
    return empAdd1.compareTo(empAdd2);
    else
    return empName1.compareTo(empName2);
    }
    };

    public String toString(){
    return empName + ":"+ empAdd;
    }
    }

    public class MyCollection{
    public MyCollection() {
    super();
    }
    public static void main(String[] args) {
    ArrayList a = new ArrayList();

    Person p = new Person("A","C");
    a.add(p);
    p=null;

    p = new Person("A","A");
    a.add(p);
    p=null;

    p = new Person("C","B");
    a.add(p);
    p=null;
    Object[] o = a.toArray();

    Arrays.sort(o,Person.sortByEmpName);

    for(int i =0; i System.out.println((Person)o[i]);

    }
    }


    I think you can check this programm. you will get help from it.
  • Regarding sorting of objects
    2006-11-28 23:26:24  raoul.schmidiger [View]

    Delegate the compareTo to Strings implementation:


    public int compareTo(Object o) {
    ...
    return this.getText().compareTo(o.getText());


    Regards Raoul