• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Need Help in Sorting Multiple columns!

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Guys,

I am working on displaying the data in the table (in 5 colums, and number of rows depends on data in database). All the table headers are hyperlinks. When any header is clicked, all the data in the table should be sorted, based on the clicked column. When the same header is clicked again, the table should be sorted in reverse order.

I have all the data in the ArrayList, which contains the persistant Objects. Each persistant Object represents data of one row.

Can any one give me the idea, how to implement sorting in above situation? Or any suggestions are welcome.

Thanks,
Bhanu
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can sort the elements in an ArrayList using the java.util.Collections.sort() method.

You will have to implement a java.util.Comparator and to get them in reverse order you can use a Collections.reverseOrder() method to get a reversed Comparator.

It is a good idea if you implement also java.lang.Comparable as well as override equals() and hashCode() methods.

Another interesting option I have seen is the used of GlazedLists

I hope this helps.

Best regards,
Edwin Dalorzo.
[ April 15, 2006: Message edited by: Edwin Dalorzo ]
 
Bhanu S Chatta
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the response Edwin! Glazed lists won't be helpful to me as I am not using any event hangling. I know, Collections, Arrays will be helpful. Only Concern is, the elements in ArrayList are Objects, and Each Object contains the original data to be displayed in table. Does the Collections etc help me in this situation?

Thanks,
Bhanu
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's take a look an example and you tell me if it might be helpful

 
Bhanu S Chatta
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am giving my problem in some more detail...


public class StoreObject{

private short region;
private int tab;
private int store;
private String storeName;
private int projectNum;

public short getRegion(){
return region;
}

public int getTab(){
return tab;
}

public int getStore(){
return store;
}

public String getStoreName(){
return storeName;
}

public int getProjectNum(){
return projectNum;
}

public void setRegion(short aregion){
region=aregion;
}

public void setTab(int atab){
tab=atab;
}

public void setStore(int astore){
store=astore;
}

public void setStoreName(String aStoreName){
storeName=aStoreName;
}

public void setProjectNum(int aProjectNum){
projectNum=aProjectNum;
}

}

The Values from database will be stored into above Object, through set methods.

So, the ArrayList[StoreObject1, StoreObject2, StoreObject3, StoreObject4, StoreObject5....]

So, I have data in above form, which I am displaying in a table form by using Iterator for getting values from StoreObject. Now I need to sort the table. Can you give me some idea?
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, what about this, which is pretty much what I already said:

I create a comparator for every field I want to sort the list by.
Then I sort the list with the comparator.
Then I use the iterator.
The reverse order I get it using a reversed comparator obtain from the Collections.reverseOrder method.

I am using your object this time.



If table your are talking about is a html table, you will have to do a post every time the user wants to sort the table. In that post pass the information with the particular column you want to sort the table by and use it to pick the right comparator you want to use.
[ April 15, 2006: Message edited by: Edwin Dalorzo ]
 
Bhanu S Chatta
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Edwin Dalorzo:
List<StoreObject> list = new ArrayList<StoreObject>();


//Once sorted use your iterator here
ListIterator<StoreObject> iter = list.listIterator();



//Sort reversed
Comparator<StoreObject> ByReversedRegion;

}

public static Comparator<StoreObject> ByRegion = new RegionComparator();
public static Comparator<StoreObject> ByStoreName = new StoreNameComparator();


//One comparator for every field you want to sort the table
public static class RegionComparator implements Comparator<StoreObject>{

[ April 15, 2006: Message edited by: Edwin Dalorzo ][/QB]



Edwin, Thanks a lot for clear demonstration! I have one quick question, I never used anglebrackets(i.e.< & > ) in declaration of the variable or Object. Is it a new concept and must while doing the Object ordering? From which version of J2SE it's introduced? Or Can this be used in all versions of Java API's? Just trying to know...

Thanks,
Bhanu
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bhanu, comrade. The angle brackets are part of a concept named Generics. It is a very powerful concept introduced in Java Tiger (JDK 1.5).

If you are using Mantis (JDK 1.4) or an older version of the JDK you will need to remove the angle brackets.

I recommend you to take a look at the Generics Tutorial

Good luck, pal and succeed with your algorithm!
 
Bhanu S Chatta
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Edwin!! I will go through the generics tutorial. I am using older version of JDK. So, I need to remove angle brackets and make sure objects are compatible before casting...right?

Best Regards,
Bhanu
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's correct, Bhanu, comrade. Generics avoid explicit casting in certain cases, and you will have to remove the brackets in the code and, most probably, use some explicit casting.

Good luck, pal!
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


But worth it I hope.

This may be useful to a great many people: jmsorter

It allows sorting of any List by either using custom sorting where you define the fields/order by which they are to be sorted, or by annotating the fields you want for the same effect.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic