This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sorting JTable

 
sparks
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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
 
Jeff Langr
author
Ranch Hand
Posts: 799
5
  • Mark post as helpful
  • send pies
  • 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 ]
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • 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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic