HANDLING STRINGS


SOURCE CODE:

//StringOrdering.java

public class StringOrdering
{
Static String name[]={"CHENNAI","TIRUNELVELI","DELHI","BOMBAY","CALCUTTA"};
       public static void main(String args[])
       {
          int size=name.length;
          String temp=null;
          System.out.println("\n........Before Sorting.......\n");
          for(int i=0;i<size;i++)                    
          {
            System.out.println(name[i]);
          }
          for(int i=0;i<size;i++)
          {
            for(int j=i+1;j<size;j++)
            {
              if(name[j].compareTo(name[i])<0)
              {
                temp=name[i];
                name[i]=name[j];
                name[j]=temp;
              }
            }
          }
          System.out.println("\n.........After Sorting........\n");
          for(int i=0;i<size;i++)
          {
            System.out.println(name[i]);
          }
       }
    }

OUTPUT :

  ........Before Sorting.......

  CHENNAI
  TIRUNELVELI
  DELHI
  BOMBAY
  CALCUTTA

  .........After Sorting........

  BOMBAY
  CALCUTTA
  CHENNAI
  DELHI
  TIRUNELVELI
Previous
Next Post »

Still not found what you are looking for? Try again here.