• 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

Sort an array alphabetically and by length

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is it s1.equals(s2)?
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??

 
Sheriff
Posts: 22783
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
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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh ok. I get it now. Thanks for the explanation and the tips for improvement!
 
Rob Spoor
Sheriff
Posts: 22783
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic