How to arrange my array in alphabetical order?

Hi,

I'm having some trouble trying to organize my String array contents into alphabetical order.

like something like this:

Boar

Dog

Dragon

Horse

Here's my code so far:

-but the way I have arranged it so far, is only by the number of characters in the string

publicclass TaskSorter

{

//ByTitle bt;

//ByDueDate dd;

String[] test ={"Rat","Ox","Tiger","Rabbit","Dragon","Snake",

"Horse","Sheep","Monkey","Rooster","Dog","Boar"};

public TaskSorter()

{

}

publicstaticvoid main(String[] args)

{

(new TaskSorter()).title();

//(new TaskSorter()).duedate();

}

privatevoid title()

{

ByTitle bt =new ByTitle ();

Arrays.sort(test, bt);

System.out.println("- Title Order -");

for (int i = 0; i < test.length; i++)

{

System.out.println(test[i]);

}

}

publicstaticclass ByTitleimplements Comparator

{

publicint compare(Object o1, Object o2)

{

return o1.toString().length() - o2.toString().length();

}

}

}

[2634 byte] By [vopoa] at [2007-11-27 9:22:34]
# 1
Well, yeah, you're telling your comparator to only use the lengths of the strings to compare. What else would you expect it to do?String implements Comparable anyway, so you don't have to provide a comparator if you're sorting an array of Strings alphabetically.
jverda at 2007-7-12 22:17:04 > top of Java-index,Java Essentials,Java Programming...
# 2
get rid of the comparator. String arrays can sort themselves alphabetically.addendum: dang, too slow!Message was edited by: petes1234
petes1234a at 2007-7-12 22:17:04 > top of Java-index,Java Essentials,Java Programming...
# 3
but is it possible though for a comparator to be created to be used in an alphabetical order?
vopoa at 2007-7-12 22:17:04 > top of Java-index,Java Essentials,Java Programming...
# 4
Yes but why reinvent the wheel?As it has already been pointed out you are comparing the strings lengths not lexigraphically. Therefore according to you "caterpillar" comes after "fox".
floundera at 2007-7-12 22:17:04 > top of Java-index,Java Essentials,Java Programming...
# 5

sure, but it is something simple, redundant and completely unnecessary:

public class StringComparator implements Comparator<String>

{

public int compare(String arg0, String arg1)

{

return arg0.compareTo(arg1);

}

}

petes1234a at 2007-7-12 22:17:04 > top of Java-index,Java Essentials,Java Programming...
# 6
HiBut, what it seems like no?...inorder to sort String array in lexicographical order using Arrays, u dont need any comparator at all... It is the assumed behaviour of the sort for String arrays....Try with out any comparatorSajid
mohammedsajida at 2007-7-12 22:17:04 > top of Java-index,Java Essentials,Java Programming...
# 7

> But, what it seems like no?...inorder to sort String

> array in lexicographical order using Arrays, u dont

> need any comparator at all... It is the assumed

> behaviour of the sort for String arrays....

>

> Try with out any comparator

>

> Sajid

If you read above Sajid, you will see that both flounder and I have already said this very thing in this very thread.

petes1234a at 2007-7-12 22:17:04 > top of Java-index,Java Essentials,Java Programming...
# 8
The reason I asked in a comparator form, is that my instructor wanted there in my code.I don't know why though, but those are his requirements.
vopoa at 2007-7-12 22:17:04 > top of Java-index,Java Essentials,Java Programming...
# 9
Then it's no bad thing that you try out a few different orderings, for example reverse order.
malcolmmca at 2007-7-12 22:17:04 > top of Java-index,Java Essentials,Java Programming...
# 10
Note that java.util.Collections has a method to give you a reverse comparator, given a comparator argument.
paulcwa at 2007-7-12 22:17:04 > top of Java-index,Java Essentials,Java Programming...