954,545 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

Sorting Strings

0
By Bill Anderson on Sep 20th, 2004 6:07 pm

This class will sort strings in alphabetical order.

public class SortArrayString
{
public static void sortEm(String [] array, int len)
	{
		
		int a,b;
	    String temp;
		int sortTheStrings = len - 1;
		for (a = 0; a < sortTheStrings; ++a)
		for (b = 0; b < sortTheStrings; ++b)
		if(array[b].compareTo(array[b + 1]) >0)
		{
		  temp = array[b];
		  array[b] = array[b + 1];
		  array[b + 1] = temp;	
	    	}
	
		  }
		}

Thank you. :)

cscgal
The Queen of DaniWeb
Administrator
19,425 posts since Feb 2002
Reputation Points: 1,474
Solved Threads: 230
 

A far easier implementation is as follows:

public static void sortEm(String[] array, int len)
{
    java.util.Arrays.sort(array);
}
jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

Hah... I had to register to this website just to find out that I can use an already existing method to sort Strings into alphabetical order... thanks jwenting.:D

agent_x91
Newbie Poster
1 post since Dec 2005
Reputation Points: 10
Solved Threads: 0
 

Why did you have to register? Guests have the ability to view code snippets and comments.

cscgal
The Queen of DaniWeb
Administrator
19,425 posts since Feb 2002
Reputation Points: 1,474
Solved Threads: 230
 

..its good to see how really sorting goes on!..
if we use java.util.Arrays.sort(array);
it makes us faster and easier!.,..
yah its much easier but this one is good to letting know your self on how sorting goes on!!
this help most students like me!!!..
well, its up to you on what you better used to be,.
the point is you understand how it goes like that and goes like this and so on and so for.....,;

alpe gulay
Light Poster
45 posts since May 2008
Reputation Points: 9
Solved Threads: 1
 

so the array variable now has sorted??and i just use that array?

madaapril
Newbie Poster
3 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

java.util.Arrays.sort(array);
Yes, this sorts the contents of array. After this call the array is in sorted order, so you can just use it.
If you have more questions please start a new topic.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

does this sorting method also capable in handling more than two arrays??

pokejef143
Newbie Poster
1 post since Feb 2012
Reputation Points: 10
Solved Threads: 0
 
does this sorting method also capable in handling more than two arrays??


i guess it does you'd just have to call it twice, once for each array to sort. but please as said above:
If you have more questions please start a new topic.

DavidKroukamp
Practically a Master Poster
Team Colleague
693 posts since Dec 2011
Reputation Points: 282
Solved Threads: 169
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You