Java Program to Sort List of Strings

Here is the Java program to sort rundown of strings. This program utilizes compareTo() strategy to look at the strings lastly utilizes a bubble sort method to sort them.

compareTo() technique thinks about two strings and returns integer esteem. The returned integer esteem is interpreted as shown beneath.

Under zero: first string is not exactly second

More noteworthy than zero: first string is more prominent than second

Zero: two strings are equivalent

Program

class StringsSortingExample
{
	public static void main(String...s)
	{
		int n,i,j;
		String str[]={"you","are","so","cute","person"};
		
		System.out.println("Before Sorting:");
		for(i=0;i<str.length;++i)
			System.out.println(str[i]);
	
		for(i=0;i<str.length;++i)
		{
			for(j=0;j<(str.length-i-1);++j)
			{
				if(str[j].compareTo(str[j+1])>0)
				{
					String temp;
					temp=str[j+1];
					str[j+1]=str[j];
					str[j]=temp;
				}
			}
		}
 
		System.out.println("\nAfter Sorting:");
		for(i=0;i<str.length;++i)
			System.out.println(str[i]);
	}		
}

Output

Java Program to Sort List of Strings

Leave a Comment

error: Alert: Content is protected!!