• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sorting ArrayList

 
Kd Martin
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 453
Google Web Toolkit Hibernate Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Martin,

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

Thanks
 
Kd Martin
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would I go about sorting these based on averageRating then?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20724
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
make the chnage in the following statement


ArrayList<Rental> games = new ArrayList<Rental>();
 
Campbell Ritchie
Sheriff
Posts: 48381
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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>();
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic