• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Comparator Interface Question

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I have created a class laptop as below



I have also created a LaptopManager class, the objective is to manage a list of Laptops as below




After doing this, I created a SortComparator class which implements the comparator interface as below




Now, in my runner class I want to sort a list of Laptops stored in the ArrayList in StudentManager......



My question is can this be done in my runner class, knowing I want to keep this list fully encapsulated in the LaptopManager Class......

Any help appreciated......
 
Marshal
Posts: 76868
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't inactivate HTML; that stopped your code tags from working.

Of course you can sort like that. But don't say you are sorting things in your runner class. You don't do anything in a class, only in its methods. And you can use different Comparators. But Sort isn't a good name for a Comparator at all, least of all when you called the class something different: SortComparator. Which is no better a name. Neither name tells you what the sorting does.
Challenge: go through the Comparator interface and find one of its methods with comparing in its name, and use that to create your Comparator as a λ preferably with a method reference.
 
Michael Curley
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Thanks for your reply. I appreciate the feedback.

Thanks for the pointer. I must take a closer look at the Comparator interface, and see if I can work out the method you are talking about.

Best regards

 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also check on the Collections.sort method.
It takes two parameters, a List and a Comparator.  You are supplying the Comparator, but a LaptopManager is not a List.

One option there is that the LaptopManager has a sort method that accepts a Comparator and returns a sorted List of Laptops.
 
Saloon Keeper
Posts: 5176
207
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agree with Dave. I would even go one step further. Let the LaptopManager have a method 'void sortBy(String what), that sorts the List. The String 'what'  could be "name" or "price". The method can be overloaded to also accept a Comparator.
 
Michael Curley
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dave/Piet

Thank you guys so much. That's exactly what I was looking for :-)

Best Regards
 
Michael Curley
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Some further info about what way I was approaching the problem. I realised that I was not supplying a list to the Collections.sort method, so I made a copy of the original list and returned this from a new method in Laptop Manager class. However, this did not work either for some reason, and I spent most of last night trying to figure out why. However, I am going to try the approach you have suggested.

Thanks guys
 
Piet Souris
Saloon Keeper
Posts: 5176
207
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Michael.

you're welcome!

The way you defined the Comparator is how we did it beforw java 8. Campbell hinted towards using a java8+ way, with lambdas et all. The subject hasn't come up since, but I wanted to show the current way of defining Comparators. This code is just for demo purposes of course.

 
If you try to please everybody, your progress is limited by the noisiest fool. And this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic