Home > Java > Java Program to sort an array of strings – Q20

Java Program to sort an array of strings – Q20

January 10, 2009 Leave a comment Go to comments

Q20: Java Program to sort an array of strings

class SortString{
	static String arr[] = { "Now", "is", "the", "time", "for", 
    "all", "good", "men", "to", "come", 
    "to", "the", "aid", "of", "their", 
    "country" };
	
	public static void main(String args[]){
		for(int j=0; j<arr.length; j++){
			for(int i=j; i<arr.length; i++){
				if(arr[i].compareTo(arr[j]) < 0){
					String t = arr[j];
					arr[j] = arr[i];
					arr[i] = t;
				}
			}
			System.out.println(arr[j]);
		}
	}
}

… from College notes (BCA/MCA assignments):


Advertisement
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: