sorting an ArrayList by int

Discussion in 'Java' started by zcraven, Oct 25, 2004.

  1. zcraven

    zcraven Guest

    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! :)
     
    zcraven, Oct 25, 2004
    #1
    1. Advertising

  2. zcraven <> scribbled the following
    on comp.lang.java.programmer:
    > 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! :)


    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.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-------------------------------------------------------- rules! --------/
    "'I' is the most beautiful word in the world."
    - John Nordberg
     
    Joona I Palaste, Oct 25, 2004
    #2
    1. Advertising

  3. zcraven

    zcraven Guest

    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



    "Joona I Palaste" <> wrote in message
    news:cljeut$5m9$...
    > zcraven <> scribbled the following
    > on comp.lang.java.programmer:
    > > 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! :)

    >
    > 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.
    >
    > --
    > /-- Joona Palaste () ------------- Finland --------\
    > \-------------------------------------------------------- rules! --------/
    > "'I' is the most beautiful word in the world."
    > - John Nordberg
     
    zcraven, Oct 25, 2004
    #3
  4. zcraven

    Carl Howells Guest

    zcraven wrote:
    > 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.
     
    Carl Howells, Oct 25, 2004
    #4
  5. zcraven

    zcraven Guest

    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)







    "zcraven" <> wrote in message
    news:cljffa$jog$...
    > 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
    >
    >
    >
    > "Joona I Palaste" <> wrote in message
    > news:cljeut$5m9$...
    > > zcraven <> scribbled the following
    > > on comp.lang.java.programmer:
    > > > 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! :)

    > >
    > > 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.
    > >
    > > --
    > > /-- Joona Palaste () -------------

    Finland --------\
    > > \--------------------------------------------------------

    rules! --------/
    > > "'I' is the most beautiful word in the world."
    > > - John Nordberg

    >
    >
     
    zcraven, Oct 25, 2004
    #5
  6. zcraven

    Eric Sosman Guest

    zcraven wrote:
    > 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().

    --
     
    Eric Sosman, Oct 25, 2004
    #6
  7. zcraven

    Andy Hill Guest

    "zcraven" <> wrote:
    >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().
     
    Andy Hill, Oct 25, 2004
    #7
  8. zcraven

    zcraven Guest

    "Carl Howells" <> wrote in message
    news:...
    > zcraven wrote:
    > > 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.


    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.
     
    zcraven, Oct 25, 2004
    #8
  9. zcraven

    zcraven Guest

    "Eric Sosman" <> wrote in message
    news:cljggs$8$...
    > zcraven wrote:
    > > 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().
    >
    > --
    >
    >


    The Club class should not abstract so it seems I cannot include the line
    'implements Comparable'
     
    zcraven, Oct 25, 2004
    #9
  10. zcraven

    zcraven Guest

    "zcraven" <> wrote in message
    news:cljh01$c3f$...
    >
    > "Eric Sosman" <> wrote in message
    > news:cljggs$8$...
    > > zcraven wrote:
    > > > 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().
    > >
    > > --
    > >
    > >

    >
    > 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.
     
    zcraven, Oct 25, 2004
    #10
  11. zcraven wrote:
    > 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
     
    Steve Horsley, Oct 25, 2004
    #11
  12. "zcraven" <> writes:

    > Club club = league; // ERROR 'array required but found


    You cannot use array syntax for accessing List elements: Use get(i).
     
    Tor Iver Wilhelmsen, Oct 25, 2004
    #12
  13. zcraven

    Oscar kind Guest

    In comp.lang.java.help zcraven <> wrote:
    > 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.


    --
    Oscar Kind http://home.hccnet.nl/okind/
    Software Developer for contact information, see website

    PGP Key fingerprint: 91F3 6C72 F465 5E98 C246 61D9 2C32 8E24 097B B4E2
     
    Oscar kind, Oct 25, 2004
    #13
  14. zcraven

    Oscar kind Guest

    In comp.lang.java.help zcraven <> wrote:
    > 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.


    --
    Oscar Kind http://home.hccnet.nl/okind/
    Software Developer for contact information, see website

    PGP Key fingerprint: 91F3 6C72 F465 5E98 C246 61D9 2C32 8E24 097B B4E2
     
    Oscar kind, Oct 25, 2004
    #14
  15. zcraven

    sks Guest

    "zcraven" <> wrote in message
    news:cljgrp$c3f$...
    >
    > "Carl Howells" <> wrote in message
    > news:...
    > > zcraven wrote:
    > > > 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.

    >
    > 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.
     
    sks, Oct 25, 2004
    #15
  16. zcraven

    zcraven Guest

    "Steve Horsley" <> wrote in message
    news:cljhkp$da2$2surf.net...
    > zcraven wrote:
    > > 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
    >


    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
     
    zcraven, Oct 25, 2004
    #16
  17. zcraven

    Hal Rosser Guest

    "zcraven" <> wrote in message
    news:clje6e$6qt$...
    > 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


    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.781 / Virus Database: 527 - Release Date: 10/21/2004
     
    Hal Rosser, Oct 25, 2004
    #17
  18. In article <cljhkp$da2$2surf.net>,
    Steve Horsley <> wrote:
    >
    >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
    --
    Bent Dalager - - http://www.pvv.org/~bcd
    powered by emacs
     
    Bent C Dalager, Oct 26, 2004
    #18
  19. zcraven

    zcraven Guest

    "Bent C Dalager" <> wrote in message
    news:cll6s4$8i8$...
    > In article <cljhkp$da2$2surf.net>,
    > Steve Horsley <> wrote:
    > >
    > >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.
    >


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

    Zac
     
    zcraven, Oct 26, 2004
    #19
  20. zcraven wrote:
    > "Bent C Dalager" <> wrote in message
    > news:cll6s4$8i8$...
    >
    >>In article <cljhkp$da2$2surf.net>,
    >>Steve Horsley <> wrote:
    >>
    >>>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.
    >>

    >
    >
    > 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.
     
    Steve Horsley, Oct 26, 2004
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Homa
    Replies:
    4
    Views:
    9,949
  2. Schnoffos
    Replies:
    2
    Views:
    1,251
    Martien Verbruggen
    Jun 27, 2003
  3. Hal Styli
    Replies:
    14
    Views:
    1,709
    Old Wolf
    Jan 20, 2004
  4. arun
    Replies:
    8
    Views:
    481
    Dave Thompson
    Jul 31, 2006
  5. aling
    Replies:
    8
    Views:
    1,032
    Jim Langston
    Oct 20, 2005
Loading...

Share This Page