Sorting Strings

Banderson

This class will sort strings in alphabetical order.

294 Views
About the Author

Happily married , Certified Java Engineer.

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;	
	    	}
	
		  }
		}
Dani 2,239 The Queen of DaniWeb Administrator Featured Poster Premium Member

Thank you. :)

jwenting 1,649 duckman Team Colleague

A far easier implementation is as follows:

public static void sortEm(String[] array, int len)
{
    java.util.Arrays.sort(array);
}
agent_x91 0 Newbie Poster

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

Dani 2,239 The Queen of DaniWeb Administrator Featured Poster Premium Member

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

alpe gulay -1 Light Poster

..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.....,;

madaapril 0 Newbie Poster

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

JamesCherrill 4,248 Most Valuable Poster Moderator Featured Poster

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.

pokejef143 0 Newbie Poster

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

DavidKroukamp 105 Master Poster Team Colleague Featured Poster

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.

shivakr 0 Newbie Poster

i want to code for "Flames" a famous game which compare to persons names and give the result as
F for Friends
L for Lovers
A for argant
M for Marriage
E for Enemy
S for Sister.......
plz just give the hints i can proceed......

JamesCherrill 4,248 Most Valuable Poster Moderator Featured Poster

DaniWeb Member Rules include:
"Do not hijack old threads by posting a new question as a reply to an old one"
http://www.daniweb.com/community/rules
Please start your own new thread for your question

This thread is closed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.