PROGRAM TO SORT THE NAMES OF THE STUDENTS OF A CLASS

//PROGRAM TO SORT THE NAMES OF THE STUDENTS OF A CLASS
class sortNames
{
public static void main(String args[ ])
{
String temp=null;
String name[ ]={“jatin”,”tushar”,”ankur”,”amit”};
int l,i,j;
l=name.length;
System.out.println(“Unsorted names of the students are: “);
for(i=0;i<l;i++)
{
System.out.println(name[i]);       //PRINTING UNSORTED NAMES
}
for(i=0;i<l;i++)
{
for(j=i+1;j<l;j++)
{
if(name[i].compareTo(name[j])>0)     //SORTING ELEMENTS
{
temp=name[i];
name[i]=name[j];
name[j]=temp;

}
}
}
System.out.println(“Sorted names of the studens are: “);
for(i=0;i<l;i++)
{
System.out.println(name[i]);        //PRINTING SORTED ELEMENTS
}
}
}

Leave a Reply

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