• 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

Sorting JTable

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all (first post)
I have created a small program that acts as a simple football league buti am having difficulty sorting the JTable where the teams are displayed.
I have two classes:
1. Input - Enters result and displays a list of results entered
2. Output - Displays a league table where team data is stored (i.e. Team name, games won/drawn/lost, goals for/against, goal difference, points)
I have some code to sort the table but am unsure if it is correct and where it should be placed. I know collections.sort(table); can be used with a bunch of if statements:
int result;
if (points > t.points) || (points == t.points) && (gd > t.gd) || (points == t.points) && (gd > t.gd) && (goalsFor > t.goalsFor)
{
result -1
}
else
{
result 1
}
return result
}
I know this needs more work but where this is to be placed is confusing.
Any help would be grateful, thanks
 
author
Posts: 799
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch. Just FYI, the moderators will ask you to supply a real first and last name.
First have your Team class implement the Comparable interface. This means supplying a compareTo method that does the job of your long "if" expression (I expressed the same condition differently):

Then in your AbstractTableModel subclass, implement a sort method you can call:

If you want to get more complex and allow sorting by different columns based on what the user clicks, you can create classes that implement the interface java.util.Comparator (q.v.).
You should start by looking at the API documentation for the Comparable interface. Then take a look at the sort methods in the Collections class.
-Jeff-
[ March 25, 2004: Message edited by: Jeff Langr ]
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sparks,
Welcome to JavaRanch!
We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.
Thanks Pardner! Hope to see you 'round the Ranch!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic