| 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...
|
||
Showing messages 1 through 3 of 3.
-
Regarding sorting of objects
2006-12-20 02:18:03 hirenaik [View]
-
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 -
Regarding sorting of objects
2006-11-29 01:14:09 raoul.schmidiger [View]
for sorting with 2 criterias, you will have to first sort by one criteria and then within each "element" do a second sort with the other criteria.
sort1: 1, 2, 3 ... n
sort2 1: a, b, c ..., z
sort2 2: a, b, c ..., z
sort2 3: a, b, c ..., z
sort2 ...: a, b, c ..., z
sort2 n: a, b, c ..., z
Easy...



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
}
}
I think you can check this programm. you will get help from it.