Java program to sort a given list of names in ascending order

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class sorting
{
	public static void main(String[] input)
	{
		int k=input.length;
		String temp=new String();
		String names[]=new String[k+1];
		for(int i=0;i<k;i++)
		{
			names[i]=input[i];
		}
		for(int i=0;i<k;i++)
			for(int j=i+1;j<k;j++)
			{
				if(names[i].compareTo(names[j])<0)
				{
					temp=names[i];
					names[i]=names[j];
					names[j]=temp;
				}
			}
System.out.println(“Sorted order is”);
for(int i=0;i<k;i++)
{
			System.out.println(names[i]);
}
     }
}

Output:

Java sorting Harish Ramesh Mahesh Rakesh
Sorted order is
Ramesh
Rakesh
Mahesh
Harish

Java sorting sai hari teja ravi sandeep
Sorted order is
teja
sandeep
sai
ravi
hari


43 Responses to “Java program to sort a given list of names in ascending order”

Leave a Reply