sorting an ArrayList by int

Z

zcraven

I need to sort a football league by the number of points each club has. The
clubs are objects stored in an ArrayList called 'league'. Each club has a
field for the number of points they have.

Can someone suggest the best way to sort this arraylist and output it to
screen?

Most examples on the web suggest I type:

collections.sort(league)

but i get the error that it expects an Array but has found an ArrayList.

PLEASE HELP! :)
 
J

Joona I Palaste

zcraven <[email protected]> scribbled the following
I need to sort a football league by the number of points each club has. The
clubs are objects stored in an ArrayList called 'league'. Each club has a
field for the number of points they have.
Can someone suggest the best way to sort this arraylist and output it to
screen?
Most examples on the web suggest I type:

but i get the error that it expects an Array but has found an ArrayList.
PLEASE HELP! :)

There is a Collections.sort(List) method. Simply feed your ArrayList to
it with Collections.sort(league). You have to make sure your club
objects implement the Comparable interface though - otherwise you may
end up with ClassCastExceptions, or a list that is not properly sorted.
 
Z

zcraven

when I type this line in class 'Club' I get the error 'club is not abstract
and does not override abstract method compareTo':

public class Club implements Comparable

I have been stuck on this for 4 hours!

Zac
 
C

Carl Howells

zcraven said:
when I type this line in class 'Club' I get the error 'club is not abstract
and does not override abstract method compareTo':

public class Club implements Comparable

I have been stuck on this for 4 hours!

Zac

Please don't top-post.

You're getting that error because your class (class names should start
with capital letters) is not declared as abstract, and doesn't override
the abstract method compareTo. The compiler is telling you quite
clearly what's going on.

In order to implement an interface in a non-abstract class, you have to
provide implementations for the methods declared in the interface.
Comparable happens to declare one method, named compareTo. Check the
documentation for Comparable to see what the method's exact signature
should be, and what it should do when you implement it.
 
Z

zcraven

This is the whole code of the method I am writing:

public void printLeagueTable()
{
System.out.println("LEAGUE: " + leagueName);
Collections.sort(league);

for (int i=0; i<league.size(); i++)
{
Club club = league; // ERROR 'array required but found
arraylist'
String clubName = club.getClubName();
int points = club.getPointsTally();
System.out.println(clubName + " = " + points);
}
}

(the clubs in the league have other fields but I want to sort them by the
goalTally field)
 
E

Eric Sosman

zcraven said:
when I type this line in class 'Club' I get the error 'club is not abstract
and does not override abstract method compareTo':

public class Club implements Comparable

I have been stuck on this for 4 hours!

Did you implement a compareTo() method? When
you said that your class "implements Comparable,"
you promised to provide compareTo().
 
A

Andy Hill

zcraven said:
when I type this line in class 'Club' I get the error 'club is not abstract
and does not override abstract method compareTo':

public class Club implements Comparable

I have been stuck on this for 4 hours!

Zac
If you implement the Comparable interface in Club, you gotta have a "public int
compareTo(Object o)" method in Club. Lots of examples on the 'net on how best
to implement compareTo().
 
Z

zcraven

Carl Howells said:
Please don't top-post.

You're getting that error because your class (class names should start
with capital letters) is not declared as abstract, and doesn't override
the abstract method compareTo. The compiler is telling you quite
clearly what's going on.

In order to implement an interface in a non-abstract class, you have to
provide implementations for the methods declared in the interface.
Comparable happens to declare one method, named compareTo. Check the
documentation for Comparable to see what the method's exact signature
should be, and what it should do when you implement it.

I am just a beginner to java and I dont really follow what you mean in your
2nd paragraph. Basically I want to iterate all clubs in the league, getting
the number of points from each one and putting them into order. When I try
to get a club from the arraylist, it says it requires an array when I am
using an arraylist. Thanks.
 
Z

zcraven

Eric Sosman said:
Did you implement a compareTo() method? When
you said that your class "implements Comparable,"
you promised to provide compareTo().

The Club class should not abstract so it seems I cannot include the line
'implements Comparable'
 
Z

zcraven

zcraven said:
The Club class should not abstract so it seems I cannot include the line
'implements Comparable'

How about I forget ArrayList and use a different method to display the
league instead? I just need to access all the clubs, return the number of
points of each, sort these results, and then display them to the user.
 
S

Steve Horsley

zcraven said:
I need to sort a football league by the number of points each club has. The
clubs are objects stored in an ArrayList called 'league'. Each club has a
field for the number of points they have.

Can someone suggest the best way to sort this arraylist and output it to
screen?

Most examples on the web suggest I type:

collections.sort(league)

but i get the error that it expects an Array but has found an ArrayList.

PLEASE HELP! :)

I suggest that you write an implementation of the Comparator class that
goes something like (off the top of my head, not reading the docs):

public class ClubPointsComparator implements Comparator {
public int compare(Object o1, Object o2) {
Club c1 = (Club) o1;
Club c2 = (Club) o2;
return c2.points - c1.points;
}
}

and then compare like this:

Collections.sort(league, new ClubPointsComparator());


HTH
Steve
 
O

Oscar kind

In comp.lang.java.help zcraven said:
I need to sort a football league by the number of points each club has. The
clubs are objects stored in an ArrayList called 'league'. Each club has a
field for the number of points they have.

Can someone suggest the best way to sort this arraylist and output it to
screen?

There are two good options:
1. Let a league implement Comparable is leagues are only compared/ranked
according to their point
2. Create a Comparator<League> otherwise.

Read the API for the methods you need to implement (one in each case) to
compare two leagues with the points they have.

Most examples on the web suggest I type:

collections.sort(league)

but i get the error that it expects an Array but has found an ArrayList.

Get your case correct; they suggested Collections#sort(List): note the
capital C.

Their suggestion assumed option 1 above. If you chose option 2, use
Collections#sort(List<T>, Comparator<? super T>) instead.
 
O

Oscar kind

In comp.lang.java.help zcraven said:
This is the whole code of the method I am writing:

public void printLeagueTable()
{
System.out.println("LEAGUE: " + leagueName);
Collections.sort(league);

for (int i=0; i<league.size(); i++)
{
Club club = league; // ERROR 'array required but found
arraylist'


Yes, league appearently is an array (!) of Club's. Not an ArrayList...

String clubName = club.getClubName();
int points = club.getPointsTally();
System.out.println(clubName + " = " + points);
}
}

(the clubs in the league have other fields but I want to sort them by the
goalTally field)

For this implementation, create a Comparator instead of implementing
Comparable (the method you need to implement does approximately the same).
Also, get this URL in your bookmarks if you haven't already:
http://java.sun.com/j2se/1.5.0/docs/api/

The URL contains the Java API, and tells you what your building blocks
are.

I have been stuck on this for 4 hours!

I hate to break it to you, but that's because you were not accurate in
your original problem description, and failed to read the API.

If you did, you could also have spent 4 hours on it, but you'd have solved
it by now.
 
S

sks

zcraven said:
I am just a beginner to java and I dont really follow what you mean in your
2nd paragraph. Basically I want to iterate all clubs in the league, getting
the number of points from each one and putting them into order. When I try
to get a club from the arraylist, it says it requires an array when I am
using an arraylist. Thanks.

What he's saying is, if you want your class to implement Comparable then you
have to 'complete' all the methods that the interface requires you to.
Comparable defines one method, public int compareTo(Object arg0), which you
have to put inside your class and write the code for in order to 'complete'
it. Then we say you've implemented the interface.
 
Z

zcraven

Steve Horsley said:
I suggest that you write an implementation of the Comparator class that
goes something like (off the top of my head, not reading the docs):

public class ClubPointsComparator implements Comparator {
public int compare(Object o1, Object o2) {
Club c1 = (Club) o1;
Club c2 = (Club) o2;
return c2.points - c1.points;
}
}

and then compare like this:

Collections.sort(league, new ClubPointsComparator());


HTH
Steve

THANK YOU - YOU ARE THE MAN!!!

It works perfectly now. I read loads of stuff today about writing a
seperate class for the comparator but I couldnt work out how it linked to
the club class. You explained it and now I can get on with my project - 5
hours after I got stuck on this bit.

Thanks again!

Zac
 
H

Hal Rosser

zcraven said:
I need to sort a football league by the number of points each club has. The
clubs are objects stored in an ArrayList called 'league'. Each club has a
field for the number of points they have.

Can someone suggest the best way to sort this arraylist and output it to
screen?

Most examples on the web suggest I type:

collections.sort(league)

but i get the error that it expects an Array but has found an ArrayList.

PLEASE HELP! :)

Your class needs to implement the comparable interface and override the
campareTo method.
The Arrays class doesn't know what to sort by - you have to tell it.
Sort by name? by team color? by points?
You tell it how when you implement comparable.
HTH
HAL
 
B

Bent C Dalager

I suggest that you write an implementation of the Comparator class that
goes something like (off the top of my head, not reading the docs):

public class ClubPointsComparator implements Comparator {
public int compare(Object o1, Object o2) {
Club c1 = (Club) o1;
Club c2 = (Club) o2;
return c2.points - c1.points;

This has the potential of running into an underflow problem. Not
particularly likely in this case, perhaps, but still.

You could either do an if-sequence or you might write something like
return new Integer(c1.points).compareTo(new Integer(c2.points));

Unless the code is likely to become a performance bottleneck, I prefer
this way since it more clearly spells out exactly what you're trying
to do and why. It's also less error prone.

Cheers
Bent D
 
Z

zcraven

Bent C Dalager said:
This has the potential of running into an underflow problem. Not
particularly likely in this case, perhaps, but still.

You could either do an if-sequence or you might write something like
return new Integer(c1.points).compareTo(new Integer(c2.points));

Unless the code is likely to become a performance bottleneck, I prefer
this way since it more clearly spells out exactly what you're trying
to do and why. It's also less error prone.

Interesting, can you explain how your way is different? What is an
underflow problem?

Zac
 
S

Steve Horsley

zcraven said:
Interesting, can you explain how your way is different? What is an
underflow problem?

Zac
He's quite right. If c2.points is -1.5 billion and c1.points is
+1.5 billion the the result should be -3 billion. But since the result
is an unsigned int (range +/- 2 billion) then the returned value would
be +1 billion instead, and the clubs would be sorted in the wrong order.

Not likely with football club points, true, but Bent has indeed spotted
a logical flaw in my code. This would not suffer the same problem:

public int compare(Object o1, Object o2) {
Club c1 = (Club) o1;
Club c2 = (Club) o2);
if (c2.points > c1.points) {
return 1;
}
else if (c2.points == c1 points) {
return(0);
}
else {
return -1;
}
}

This is OK for a comparator because only the SIGN of the result is
used to decide the sorting order (or zero of course). And it cannot
wrap and return the wrong sign result.

Steve.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top