Hello All, I would like to know if anyone has any full working examples of sorting a jtable on multiple columns. I have the following data items in a table. When the user clicks on the market column, the results should be sorted by market, code,sequence like.. market media code sequence BF TV 7 1 CA LM 2 1 CA LM 5 1 CA LM 5 2 HT TV 1 1 HT TV 1 2 HT TV 1 3 NY OD 3 1 if the user sorts on media, the code should be ordered by media, code, sequence.. market media code sequence CA LM 2 1 CA LM 5 1 CA LM 5 2 NY OD 3 1 BF TV 7 1 HT TV 1 1 HT TV 1 2 HT TV 1 3 The problem is that I have multiple rows in the table that relate to each other and therfore need to keep them together on any sort that is done. The rows need to be always sorted first by clicked column and then by code and sequence. I am using the sort logic from this site as a starter.. http://www2.gol.com/users/tame/swing/examples/JTableExamples5.html It only allows sorting on one column.
each row should be wrapped in an Object that implements comparable in the way you expect the sorting algorithm in the table to work. that means you can specify the attribute that is first checked on a comparison, than the other attributes in the order you wish. you will have to write your own comparator that takes the attribute as an argument. in the table model the row objects are placed in a List. Whenever a sort is requested you can do a Collections.sort(List, Comparator) and reload the data into the jtable. chantal