aspose file tools*
The moose likes Java in General and the fly likes Sort an array alphabetically and by length Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Sort an array alphabetically and by length" Watch "Sort an array alphabetically and by length" New topic
Author

Sort an array alphabetically and by length

Andrew Robinson
Greenhorn

Joined: Jan 06, 2010
Posts: 15
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30530
    
150

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Andrew Robinson
Greenhorn

Joined: Jan 06, 2010
Posts: 15
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30530
    
150

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

Joined: Jan 06, 2010
Posts: 15
is it s1.equals(s2)?
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30530
    
150

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

Joined: Jan 06, 2010
Posts: 15
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

Joined: Jan 06, 2010
Posts: 15
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

Joined: Oct 27, 2005
Posts: 19693
    
  20

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".


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Andrew Robinson
Greenhorn

Joined: Jan 06, 2010
Posts: 15
oh ok. I get it now. Thanks for the explanation and the tips for improvement!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

You're welcome.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sort an array alphabetically and by length