Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Generically sorting based on multiple criteria, and multiple comparators?  RSS feed

 
Jose Castillo
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey folks, I've been scratching my head over this for a bit, and was looking for some help and input.

I have implemented several sorting algorithms for class. One of them must be a mutli-leveled sort. So if I am sorting an array of People, I might be handed a salary comparator, an ID comparator, and a hire date comparator. In this case, I would be expected to sort my people array first by salary, the equal salaried by ID, then the equally salaried and ID'd by hire date. I may receive between 2 and 4 valid comparators

One thing I was trying, but did not seem to work is having overloaded methods. If I only get passed 2 comparators it's a different method than if I get passed 3. Then, have code kind of like:



I'm not allowed to use or import the default java.util.comparator (I had to write a custom interface for comparator.java). I can't use Collections.sort either, like many methods I see use

 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jose,
Look at the signature for main, the parameter list is "(String[], args)". You can use that, it is a way of getting variable input, but there is a more direct way of doing it with a variable length parameter list...

Les
BTW: I've not seen this for a long time, but check the specifics. It may give you exactly what you want to do.

Jose Castillo wrote:Hey folks, I've been scratching my head over this for a bit, and was looking for some help and input.

I have implemented several sorting algorithms for class. One of them must be a mutli-leveled sort. So if I am sorting an array of People, I might be handed a salary comparator, an ID comparator, and a hire date comparator. In this case, I would be expected to sort my people array first by salary, the equal salaried by ID, then the equally salaried and ID'd by hire date. I may receive between 2 and 4 valid comparators

One thing I was trying, but did not seem to work is having overloaded methods. If I only get passed 2 comparators it's a different method than if I get passed 3. Then, have code kind of like:



I'm not allowed to use or import the default java.util.comparator (I had to write a custom interface for comparator.java). I can't use Collections.sort either, like many methods I see use

 
Piet Souris
Rancher
Posts: 1979
67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have implemented a method: mergeSort(array, comparator), then you could make it yourself easy by
having a class, say MultiComparator that implements comparator.
The constructor could be something like (I've left the generics out, for brevity):



and as its method you could have:



Then all you nee to do is to invoke mergeSort(array, new MultiComparator...)
 
Campbell Ritchie
Marshal
Posts: 55672
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sure there is a simpler way to do that. Let us imagine a Person class with lastName, firstName and dateOfBirth fields. Let us also imagine that all those fields implement Comparable<T>.That will sort in last name, first name order, the way the military order names. In Java8 you would use a λ:-
 
Piet Souris
Rancher
Posts: 1979
67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, that is not very flexible. What if you want to change the order, or if you want to add another comparator?

I had in mind to somehow mimic the 'thenComparing' in a simple way..
 
Carey Brown
Bartender
Posts: 2979
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not entirely genericized but would something like this work for you?

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!