• 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
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Problem sorting with Comparator.comparing()

 
Bartender
Posts: 10964
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • 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
Bartender
Posts: 10964
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • 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: 5582
213
  • 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
Bartender
Posts: 10964
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • 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.
 
Rancher
Posts: 5086
82
  • 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: 5582
213
  • 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
Bartender
Posts: 10964
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • 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>
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic