source file name: stringdemo3.java
class stringdemo3
{
public static void main(String as[])
{
String[] students=new String[as.length];
String temp=null;
for(int i=0;i<as.length;i++)
students[i]=as[i];//command line students name stores in students array
System.out.println("givens student list:");
for(int i=0;i<as.length;i++)
System.out.print(students[i]+" ");
//names sorting procedure
for(int i=0;i<as.length;i++)
{
for(int j=0;j<as.length;j++)
{
int n=students[i].compareTo(students[j]);
if(n<0)
{
temp=students[i];
students[i]=students[j];
students[j]=temp;
}
}
}
System.out.println("\nsorted student list:");
for(int i=0;i<as.length;i++)
System.out.println(students[i]+"");
}
}
output:
D:\JavaPrograms\Misc>javac stringdemo3.java
D:\JavaPrograms\Misc>java stringdemo3 Mohan John Jay Abhay Suman Williams Sana Kailas
Harish Raj
givens student list:
Mohan John Jay Abhay Suman Williams Sana Kailas Harish Raj
sorted student list:
Abhay
Harish
Jay
John
Kailas
Mohan
Raj
Sana
Suman
Williams
0 Comments
Post a Comment