• Post Reply Bookmark Topic Watch Topic
  • New Topic

Sorting a List  RSS feed

 
Parijat Mukherjee
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a list of rows having a column Name , now i wan't that on click of the column header label Name the whole list of rows should get sorted in ascending order and on re click of the label the list should now get sorted in descending order . Can some one help me with the code as it is very urgent.
 
Srivani Nallan
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at the tutorial for the tables. There is an example for TableSorter. This would be a good place to start.
http://java.sun.com/docs/books/tutorial/uiswing/components/table.html
 
Parijat Mukherjee
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi srivani thanks for the link but this is not what i am looking for i was looking for a class which could extend the comparator interface and can sort a list. if some one could help
 
Srivani Nallan
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try building up on this.
------------------------
// Create a list
String[] strArray = new String[] {"z", "a", "C"};
List list = Arrays.asList(strArray);

// Sort
Collections.sort(list);
// C, a, z

// Case-insensitive sort
Collections.sort(list, String.CASE_INSENSITIVE_ORDER);
// a, C, z

// Reverse-order sort
Collections.sort(list, Collections.reverseOrder());
// z, a, C

// Case-insensitive reverse-order sort
Collections.sort(list, String.CASE_INSENSITIVE_ORDER);
Collections.reverse(list);
// z, C, a
-------------
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've created my own List which uses a TreeSet internally to store the data.
Just pass it Comparable object and it will get sorted (of course if the objects can't be compared you're in trouble, but that's a small price to pay).
It's an almost complete List implementation, I am now using it where I'd use ArrayLists before where sorting is needed.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!