Ascending and Descending In Java
public class AscendingNama {
public AscendingNama() {
}
public static void main(String[] args) {
String temp;
String nama[]={"Hery","Irawan","Andika","Aldi","Sasha" };
System.out.println("Before Ascending");
for(int i=0;i<5;i++){
System.out.println(nama[i]);
}
for(int i=0;i<4;i++)
for(int j=i+1;j<5;j++)
{
if(nama[i].compareTo(nama[j])>0) // for Descending we only change " <" in this program
{
temp=nama[i];
nama[i]=nama[j];
nama[j]=temp;
}
}
System.out.println("\nAfter ascending : ");
for(int i=0;i<5;i++)
{
System.out.println(nama[i]);
}
}
}