Comparing and sorting Strings can be a bit tricky and should be done with some care. There are some built-in functions in Java that can perform you sorting. As an example of this, if you have an array of strings, you can sort the strings via a call to Arrays.sort(), such as:


If you require the Strings to be sorted without regards to case, you’ll need a second argument, a Comparator, for Arrays.sort(). For example,

Arrays.sort(stringArray, String.CASE_INSENSITIVE_ORDER);

Similarly if you have a collection of strings, you can sort the collection via calls to:


However, in some cases, you need to develop or use your own custom sorting algorithm for sorting array of strings. A sample code of the sorting an array of string without using Array.sort() is given below.

public class Sort
  public static void main(String[] args)
    // Declare the String that is to be sorted.
    String text = "Death, be not proud, though some have called thee " +
                     "Mighty and dreadful, for thou art not so; " +
                     "For those whom thou think'st thou dost overthrow " +
                     "Die not, poor Death; nor yet canst thou kill me. " +
                     "From rest and sleep, which but thy pictures be, " +
                     "Much pleasure; then from thee much more must flow " +
                     "And sooner our best men with thee do go - " +
                     "Rest of their bones, and souls' delivery! " +
                     "Thou'rt slave to fate, chance, kings, and desperate men, " +
                     "And dost with poison, war, and sickness dwell; " +
                     "And poppy or charms can make us sleep as well " +
                     "And better than thy stroke. Why swell'st thou then? " +
                     "One short sleep past, we wake eternally, " +
                     "And Death shall be no more: Death, thou shalt die!";

    int index = 0;
    char separator = ' '; // inter-word separating character
    int count = 1;	  // Count number of words. There must be at least 1
			  // This is the reason for a do-while rather than
			  // a while loop in the example Word for Word.

    // Determine how many words there are
    while((index = text.indexOf(' ',index)) != -1)

    // Create the array of strings to contain all the words.
    String[] words = new String[count];

    index = 0;
    int endIndex = 0;

    for(int i = 0; i < count; i++)
      endIndex = text.indexOf(separator,index); // find the next separator
      if(endIndex == -1)
	words[i] = text.substring(index);
	words[i] = text.substring(index,endIndex);
      index = endIndex + 1;

    // Sort the substring array using direct insertion
      int i,j;
      String a;

      for(j = 1; j < count; j++) // Look at every word starting at 2nd
	a = words[j];		 // Put the current word in a buffer
	i = j - 1;		 // Start compare with the previous word
	while((i >= 0) && (words[i].compareTo(a) > 0))
	{			 // Compare the ith and i+1th word - if ith word
	  words[i+1] = words[i]; //  is greater than i+1th, swap them
 	  i--;                   // Decrease i so the previous pair are compared
   	}                    	 //  next time round
	words[i+1] = a;	// If any changes to the array occur, the
      }			//  word at j is gone. Put the stored word in
    }			//  the vacant place.

    // Display the sorted array of words
    for(int i = 0; i < words.length; i++)

Tagged with: JAVAProgrammingSource Code

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Related News Feeds

Set your Twitter account name in your settings to use the TwitterBar Section.