Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to arrange my array in alphabetical order?

807605Jul 1 2007 — edited Jul 2 2007
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
public class TaskSorter 
{
	//ByTitle bt;
	//ByDueDate dd;
	String[] test = {"Rat", "Ox", "Tiger", "Rabbit", "Dragon", "Snake",
			"Horse", "Sheep", "Monkey", "Rooster", "Dog", "Boar" };
	
	public TaskSorter()
	{
		
	}
	
	public static void main(String[] args) 
	{
		(new TaskSorter()).title();
		//(new TaskSorter()).duedate();
	}

	private void 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);
}
}

public static class ByTitle implements Comparator
{
public int compare(Object o1, Object o2)
{
return o1.toString().length() - o2.toString().length();
}
}
}


Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 30 2007
Added on Jul 1 2007
10 comments
313 views