• 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

Sorting Strings with comparator

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to sort strings in a file from the longest to the smallest in length but my code is not sorting.
It seems the foreach loop is using the word[] instead of the comparator. Any idea how i can solve this?

 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

A comparator is supposed to return a negative number, a zero, or a positive number, depending on whether the first argument is less than, equal to, or greater than the second argument. I do not think that is what your comparator is doing.

Henry
 
Molayo Decker
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:
A comparator is supposed to return a negative number, a zero, or a positive number, depending on whether the first argument is less than, equal to, or greater than the second argument. I do not think that is what your comparator is doing.

Henry



I tried this too it's still not working

 
Marshal
Posts: 28177
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
"Not working"? Would you care to provide some more information about that?
 
Molayo Decker
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:"Not working"? Would you care to provide some more information about that?


Thanks Paul i saved it

 
Paul Clapham
Marshal
Posts: 28177
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
All working correctly then? Looks good.
 
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here are a few inputs from my end (Read the code comments):

 
Saloon Keeper
Posts: 15490
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
salvin francis
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:


or simply modify the code as

for longest to shortest ?
 
Stephan van Hulst
Saloon Keeper
Posts: 15490
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Stephan van Hulst
Saloon Keeper
Posts: 15490
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, using the List interface and static imports, we can make the code even more fluent!
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shows how much nice the new Java8 code is Please Stephan, show what the requisite import are for those not familiar with those classes.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:



Two classes I wrote back in 2005/6 - probably along with millions of others.

Winston
 
Stephan van Hulst
Saloon Keeper
Posts: 15490
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's an example:

 
Stephan van Hulst
Saloon Keeper
Posts: 15490
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Winston, too bad your Reversed doesn't extend Reversable, in case you want to reverse your reverse ordering
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Here's an example:


Isn't it funny how we end up with different solutions to the same problem? A couple of years back I wrote an Index class which was basically a compound Comparator based on "extractors" - except I called them 'Field's and made them enums (which actually works really nicely).

I also wrote a NaturalOrder class and an Order class that included a "policy" for nulls.

All Kleenex now of course...bloody version 8...

Winston
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Winston, too bad your Reversed doesn't extend Reversable, in case you want to reverse your reverse ordering


Yeah. Reversable was more for my own stuff, so I could use both orders at the same time. I'm still not quite sure why Comparator doesn't have a reverse() method - especially after they added the 93 others for version 8.

Winston
 
Stephan van Hulst
Saloon Keeper
Posts: 15490
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:Isn't it funny how we end up with different solutions to the same problem? A couple of years back I wrote an Index class which was basically a compound Comparator based on "extractors" - except I called them 'Field's and made them enums (which actually works really nicely).

I also wrote a NaturalOrder class and an Order class that included a "policy" for nulls.

All Kleenex now of course...bloody version 8...


Yeah, I had a couple of classes lying around that went into the archives when a new Java version rolled around the corner, so I could have good laugh looking back. A utility class containing naturalOrder() was one of them.

A really weird moment was having to get rid of my SeekableChannel interface when Java 7 came out, because SeekableByteChannel was almost a verbatim copy.

I'm still not quite sure why Comparator doesn't have a reverse() method


Eh? Comparator has a static method reverseOrder(), and a non-static method reversed(). Do you mean something else?
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Eh? Comparator has a static method reverseOrder(), and a non-static method reversed(). Do you mean something else?


Yes, I mean a method that returns −compare(o1, o2) (or indeed compare(o2, o1)).

And I realise that reversed().compare() is functionally equivalent, but it's both potentially time and space consuming, and adds complexity. Why not just say that any order is implicitly reversable? Especially in these days of default implementations...

Winston
 
Sheriff
Posts: 22781
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

salvin francis wrote:


You mean this:

 
salvin francis
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:...You mean this...


Learned something Here's a Cow

No autoboxing involved in your solution. My curiosity took me to Java source for the Integer.compare static method, here is what I found :
 
Molayo Decker
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Shows how much nice the new Java8 code is Please Stephan, show what the requisite import are for those not familiar with those classes.



Campbell another why of doing this in Java 8 is this

 
Molayo Decker
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

salvin francis wrote:

Rob Spoor wrote:...You mean this...


Learned something Here's a Cow

No autoboxing involved in your solution. My curiosity took me to Java source for the Integer.compare static method, here is what I found :



Hi Salvin
I forgot the ternary operation could be used like this. Thanks...Do i get cow too? Lol
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:You mean this:


Or indeed
  return s2.length() - s1.length();

Winston
 
Rob Spoor
Sheriff
Posts: 22781
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
I prefer to never use subtraction for comparison unless I am 100% sure that overflow will not occur. That's most likely the cause for string lengths (since both are positive), but still...

An example where it can go wrong is comparing Integer.MIN_VALUE with Integer.MAX_VALUE. The difference between the two (MIN_VALUE - MAX_VALUE) is not negative as expected, but instead 1, and therefore MIN_VALUE would be considered larger than MAX_VALUE.
 
Stephan van Hulst
Saloon Keeper
Posts: 15490
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

Stephan van Hulst wrote:Eh? Comparator has a static method reverseOrder(), and a non-static method reversed(). Do you mean something else?


Yes, I mean a method that returns −compare(o1, o2) (or indeed compare(o2, o1)).

And I realise that reversed().compare() is functionally equivalent, but it's both potentially time and space consuming, and adds complexity. Why not just say that any order is implicitly reversable? Especially in these days of default implementations...

Winston



I see what you mean. In that case I would call it compareReverse(), although the only use I can think for it would be as a method handle, because in any other case you can easily just do -compare().
 
Greenhorn
Posts: 7
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have written a post to explain how sorting can be done in java. May be you will find it useful.

sort lists in java
 
Rob Spoor
Sheriff
Posts: 22781
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

T Tak wrote:I have written a post to explain how sorting can be done in java. May be you will find it useful.

sort lists in java


Your reverseSortList method doesn't sort, it only reverses. It appears to work because you let it reverse the already sorted array.

(fixed a small grammatical error...)
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is logical that min − max should be positive; max − min is −1, so swapping it round should change the sign. Let't try some binary arithmetic:-I was only using 8 bits but we can see that max − min is indeed −1.

Edit: Let's add some code:-Output:-

min = 0x80000000, max = 0x7fffffff, max - min = 0xffffffff, min - max = 0x1


 
T Tak
Greenhorn
Posts: 7
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:

T Tak wrote:I have written a post to explain how sorting can be done in java. May be you will find it useful.

sort lists in java


You're reverseSortList method doesn't sort, it only reverses. It appears to work because you let it reverse the already sorted array.



Thanks for the comment. Appreciate it. I have corrected the article. It was only reversing the list rather than reverse sorting it.
 
Rob Spoor
Sheriff
Posts: 22781
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
You're welcome.
 
machines help you to do more, but experience less. Experience this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic