• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sort an array alphabetically and by length

 
Andrew Robinson
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
anyone know an easy way to sort an array of strings by increasing length and also alphabetically within each length?

Example:

g ab jk ij lms bac akdn when sorted becomes
g ab ij jk bac lms akdn
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34965
378
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andrew,
Have you seen the Comparator class? It lets you specify how two Objects comapre. Then you can pass your comparator object to the standard sort API.
 
Andrew Robinson
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ya I currently am using it to sort by length. but I can't figure out how to do both at the same time.

This is what I have so far.

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34965
378
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note the code you wrote is equivalent to:

I only mention this because it is a little easier to see what needs to be done this way. You are checking if the sizes are different first, which is good. Then you are returning 0 regardless. Instead you want to compare the strings alphabetically. Do you know what method you call to do that?
 
Andrew Robinson
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is it s1.equals(s2)?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34965
378
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Close. That tells you if they are the same. There's another method that returns a positive, negative or zero number depending on the alphabetical order of the two strings. Hint: it begins with a "c"
 
Andrew Robinson
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
compareTo(),


but how does that help me? do I need to add ifs for that too the same way I did for the length?
 
Andrew Robinson
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it working. but it was mainly just typing in stuff and hoping for the best. Could you maybe explain why this code works the way it does??

 
Rob Spoor
Sheriff
Pie
Posts: 20659
64
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The rules about comparison are:
- return a negative number if the first object is considered to be smaller than the second
- return a positive number if the first object is considered to be larger than the second
- return 0 if the two objects are considered to be equally large

In your case there are two factors: length and lexicographical comparison, with length going first. Using your code:
- if the first string is shorter it is considered to be smaller so you return a negative number (-1)
- if the first string is larger it is considered to be larger so you return a positive number (1)
- if the two string are equally long you consider the lexicographical comparison, as the result of String.compareTo.

I have two improvements for your code:
1) replace lines 8-15 with one line: return s1.compareTo(s2). You don't need the if-else block. After all, if s1.compareTo(s2) returns a negative value you return a negative value; if it returns a positive value you return a positive value; and otherwise you return 0. Since the exact positive / negative values are irrelevant you are essentially returning the same as s1.compareTo(s2).

2) consider using s1.compareToIgnoreCase(s2). This will use case insensitive comparison. With compareTo, "G" is smaller than "a". With compareToIgnoreCase, "G" is larger than "a".
 
Andrew Robinson
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh ok. I get it now. Thanks for the explanation and the tips for improvement!
 
Rob Spoor
Sheriff
Pie
Posts: 20659
64
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic