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

Friday, October 2nd, 2009
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

Avatar Image

Author Name :
Ranjith

Total : 2 Comments


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

  1. Meek luies

    Hey, i am meek and i am thankful to you for this code of sorting learning sorting in Java language .

  2. joshua

    no m funciono man!!!! ufff q mal!!!!

Leave a Reply

Question and Answer
C/C++ Unix & Linux Wordpress
Source codes
C C++ Java

Free email signup