Krishna,
The below example uses a comparator which orders a collection of objects
based on a property. You can make the criteria more complicated if you wish.
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class Test {
public static void main(String[] args) {
Test test = new Test();
}
public Test() {
ArrayList arrayList = new ArrayList();
// The plan would have probably been read and populated from
the db
arrayList.add(new Plan());
arrayList.add(new Plan());
arrayList.add(new Plan());
Collections.sort(arrayList, new Comparator() {
public int compare(Object object1, Object object2) {
return ((Plan)
object1).getTitle().compareTo(
((Plan) object2).getTitle());
}
});
}
private class Plan {
private String name = null;
private String title = null;
private String description = null;
public String getDescription() {
return description;
}
public String getName() {
return name;
}
public String getTitle() {
return title;
}
public void setDescription(String string) {
description = string;
}
public void setName(String string) {
name = string;
}
public void setTitle(String string) {
title = string;
}
}
}
-----Original Message-----
From: Krishna Mohan Radhakrishnan [mailto:[EMAIL PROTECTED]
Sent: 10 March 2005 08:35
To: Struts Users Mailing List
Subject: Sorting of array lists
Hi all,
I have a doubt regarding sorting in array List.
I have an array List which contains array list objects of type Plan.
The Plan object contains 3 variables : plantitle ,plandescription and
planname.
I have to sort the arraylist based on the plantitle first and then based
on the plandescription.
ArrayList list = new ArrayList();
Plan planobj = null;
For (i= 1, i<=100, i++){
planObj = readPlanObjectFromDB();
list.add(planObj);
}
//?? Now how to sort it
Private Plan readPlanObjectFromDB(){
//queries the database to return plan object
return plan;
}
I am totally confused. Even if use bubble sorting it will cause a lot of
performance issues.
Could any one please suggest me a good method?
Regards,
Krishna Mohan
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]