This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
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
Author

Sorting ArrayList

Kd Martin
Ranch Hand

Joined: Nov 28, 2011
Posts: 58
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:
Martyr2,
I tried that but I get this error:

RedBox.java:82: 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:


Thanks!
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.

Thanks


SCJP 5.0 SCWCD 5.0
Kd Martin
Ranch Hand

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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
Greenhorn

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


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

Joined: Oct 13, 2005
Posts: 39084
    
  23
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>();
 
Don't get me started about those stupid light bulbs.
 
subject: Sorting ArrayList