• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Sorting ArrayList

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!
 
Ranch Hand
Posts: 453
Google Web Toolkit Hibernate Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How would I go about sorting these based on averageRating then?
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
make the chnage in the following statement


ArrayList<Rental> games = new ArrayList<Rental>();
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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>();
 
reply
    Bookmark Topic Watch Topic
  • New Topic