struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Rikesh Balan Krishnan" <>
Subject RE: Sorting of array lists
Date Thu, 10 Mar 2005 09:36:35 GMT
But all this methods are applicable only if the model class extends the
comparator interface.I too had a similar problem,but I was not able to
use the collection.sort because if the model class doesn't extend the
comparator interface, it will give a class cast exception.finally I had
to settle for a alofrithm where I wrote a sort method.I'm still looking
for some different method.

-----Original Message-----
From: Mark Benussi [] 
Sent: Thursday, March 10, 2005 2:39 PM
To: 'Struts Users Mailing List'
Subject: RE: Sorting of array lists


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

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
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)
					((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 [] 
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

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();


//?? 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?

Krishna Mohan

To unsubscribe, e-mail:
For additional commands, e-mail:

To unsubscribe, e-mail:
For additional commands, e-mail:

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message