• 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

Sort Array Of Objects Based On A Specified Property

 
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 an array of objects based on a specified property. For instance, if I had an array of movie objects called Movie[] movies, and the movie class had the properties String title, int popularity, and Date releaseDate, how would I go about writing methods that sort the array based on either title, popularity, or releaseDate?

For instance, I have this class which compares the releaseDate of two rentals:


I also have a compareTo method in the Rentals class (which is the superclass for Movies and implements Comparable) which compares two strings(titles).

How do I implement Arrays.sort for these two types of comparison methods?

Thanks!
 
Saloon Keeper
Posts: 15510
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, if I'm not mistaken, the DateComparison class will work perfectly fine for Movie as well, since Movie is a subclass of Rental. You can just call the Arrays.sort() method with the Movie array, and the DateComparison Comparator.
 
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
So would I write it likes this:
Arrays.sort(movies, DateComparison())

And then how would I call Arrays.sort for my compareTo and compare methods?
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, Arrays.sort(movies, new DateComparison());

I don't understand what you mean by your last question. Which compareTo/compare methods? What problem are you having?
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kd Martin wrote:
And then how would I call Arrays.sort for my compareTo and compare methods?



You just pass the array and the Comparator to the sort() method, as Stephan just showed you. You don't have to do anything else for the compare() method to be called. The sort() method calls it.
 
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
These methods which are in my Rental class.


How do I call them in the Arrays.sort. Is it Arrays.sort(movies, compare())? I'm having trouble understanding how to call these methods as a parameter in the Arrays.sort
 
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

Kd Martin wrote:How do I call them in the Arrays.sort. Is it Arrays.sort(movies, compare())? I'm having trouble understanding how to call these methods as a parameter in the Arrays.sort



"kd", please read the last two replies by Stephan and Jeff. They provide the complete answer. While reading them you should be aware that you are making some incorrect assumptions about the process, so be prepared to recognize them as you read.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Arrays.sort will use the compareTo method of its elements if you don't provide a Comparator. If you do, it will use the compare method of the comparator. That's how it works. To be able to sort on another field you just create a new Comparator.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This sounds like the sort of thing which a database should do; you can search for particular features in the database.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kd Martin wrote:These methods which are in my Rental class.



Yes, you already told us that.

How do I call them in the Arrays.sort. Is it Arrays.sort(movies, compare())? I'm having trouble understanding how to call these methods as a parameter in the Arrays.sort



That question was just answered a few posts ago. Simply re-asking the same question won't get you anywhere. If you didn't understand the answers, ask for clarification. Make sure to be as specific as possible about what parts of the answers you didn't understand.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic