Write a Java program for sorting a given list of names in ascending order.

//Program to sort strings in ascending order
import java.util.*;
class Sortstrings
{
            public static void main(String args[])
            {
                        String str[];
                        int n;
                        Scanner scr=new Scanner(System.in);                       
                        System.out.print("nEnter how many strings do you want to take:");
                        n=scr.nextInt();                      
                        str=new String[n];
                        System.out.println("nEnter Strings:");                      
                        for(int i=0;i<n;i++)
                        str[i]=scr.next();
                        for(int i=0;i<n;i++)
                        {
                                    for(int j=i+1;j<n;j++)
                                    {
                                                if(str[i].compareTo(str[j])>0)
                                                {         
                                                            String t=str[i];
                                                            str[i]=str[j];
                                                            str[j]=t;
                                                }
                                    }         
                        }
                        System.out.println("nSorted order of strings are:");
                        for(int i=0;i<n;i++)
                        System.out.println(str[i]);
            }
}
 
Output:
 
Enter how many strings do you want to take:3
 
Enter Strings:
zahir
koti
naveen
 
Sorted order of strings are:
naveen
koti
zahir

Leave a Reply

Your email address will not be published. Required fields are marked *

Enable Notifications OK No thanks