• 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Problem sorting with Comparator.comparing()

 
Saloon Keeper
Posts: 8222
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's an SSCCE for my issue. I need to do a sort of the Data.str field either in natural order or in reverse order depending on the value in Data.ascending. I managed to do this just fine in Data's compareTo() method, but now I'm trying to convert it to use Comarator. It mostly works but I can't figure out how to do the natural/reversed based on a value. Note that because the Data is sorted first by "ascending", by the time it needs to determine natural/reversed both arguments will be the same, either both natural or both reversed.

 
Carey Brown
Saloon Keeper
Posts: 8222
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry Piet, your suggestion didn't work, that's why I whittled the problem down to this SSCCE, it is runnable as is.
 
Saloon Keeper
Posts: 4500
166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yep. Well, it is a nasty one. I have a solution, but it is almost too ugly to show. So please don't shoot the messenger!

I could not make the Comparator static, since Data is an inner class. To get that Comparator anyway, I added an empty Constructor. And I cannot get the indentation up to standard, for some reason.
 
Carey Brown
Saloon Keeper
Posts: 8222
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your help Piet. I was unsure of the (a,b)-> syntax and you pointed me in the right direction. This works now.
 
Master Rancher
Posts: 3889
50
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just replaced the original compData declaration with:

Seems to work...

[ edit - ah, I now see it was already resolved.  I like the "compStr = (a,b) -> a.str.compareTo( b.str ) * a.reverse;" better, nice! ]
 
Piet Souris
Saloon Keeper
Posts: 4500
166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Many ways lead to Rome, as they say.

@Carey:

I could not use static, since Data is not a static class. Well, java 10 that is. Is that from a later version, or did I just screw up?
 
Carey Brown
Saloon Keeper
Posts: 8222
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm using 16 and static works. <shrugs>
 
That is a really big piece of pie for such a tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic