File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Sorting ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Sorting ArrayList" Watch "Sorting ArrayList" New topic

Sorting ArrayList

Kd Martin
Ranch Hand

Joined: Nov 28, 2011
Posts: 59
Hello. I'm having difficulty sorting an ArrayList of videogames based on their average rating. This is the code I have so far but it's not sorting correctly. And its messing up the indices. I have a class called RatingComparison that implements Comparator and sorts based on rating but I'm not sure how to implement it to sort my ArryList. Any help would be appreciated!

I tried doing Collections.Sort(games,new RatingComparison());
but I get this error:
I tried that but I get this error: cannot find symbol
symbol : method Sort(java.util.ArrayList<java.lang.Integer>,RatingComparison)
location: class java.util.Collections
Collections.Sort(games,new RatingComparison());
1 error

Here's my RatingComparison class if that helps:

Any help would be greatly appreciated!!
avi sinha
Ranch Hand

Joined: Mar 15, 2009
Posts: 453

Hi Martin,

Your comparator is supposed to deal with Rental. In other words, it can be used to sort Rental objects not Integers.


SCJP 5.0 SCWCD 5.0
Kd Martin
Ranch Hand

Joined: Nov 28, 2011
Posts: 59
How would I go about sorting these based on averageRating then?
Jeff Verdegan

Joined: Jan 03, 2004
Posts: 6109

Create an ArrayList<Rental> and call the Collections.sort(List, Comparator). After all, you don't want to sort a List of Integers, you want to sort a List of Rentals, right?
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

As for the problem you originally described: the name of the method is "sort", as in Jeff's example, and not "Sort", as in your code. Case counts in Java, so "sort" and "Sort" are different names.
suraj aryan

Joined: Nov 19, 2010
Posts: 24
make the chnage in the following statement

ArrayList<Rental> games = new ArrayList<Rental>();
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46337
Why are you adding the ratings and the count to the same List.

By the way, it should read List<Rental> games = new ArrayList<Rental>();
I agree. Here's the link:
subject: Sorting ArrayList
It's not a secret anymore!