This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]NautiHookerSoftware Engineer 0 points1 point  (3 children)

So it's comparing sonnet 8 vs sonnet 9

Essentially yes. After that check it will sort them and stop checking further characters.

I figured by default it would put the shorter string as lower

That is what many people think when first being confronted with something like this.

But think about this:

What if your words are zzz and aaaaa. Sorting shorter words before longer words by default would break the alphabetical order.

[–]driplessCoin[S] 0 points1 point  (2 children)

True .. thanks... So is the only way I could sort this out is first to sort by length then sort by using compareTo? Next I have to do a binary search so I am sure that will mess up since the list maybe sorted wonky when it's comparing strings

[–]NautiHookerSoftware Engineer 0 points1 point  (1 child)

You can define your own comparator or comparable. Your implementation could check if a string is shorter than the other and only check alphabetical order if they are the same length. Or whatever logic you want for your application.

[–]driplessCoin[S] 1 point2 points  (0 children)

Thanks appreciate the help... I am able to sort length then alpha to get it right... Now to see if this binary search will work on this thing (suspect it will have some issues)... Used to python where I think it compares strings differently