Wednesday, 4 September 2013

java program used for string sorting

import java.lang.*;

class City
{
public static void main(String args[])
{

String name[] = {"visakhapatnam","hyderabad","kolkata","mumbai","chennai","delhi","ahamdabad","calcutta","bombay"};

int size=name.length;
String temp;
    System.out.println("The total number of the strings are:" + name.length);

    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;
        }
    }
}
    for(int i=0;i<size;i++)
    {
        System.out.println(name[i]);
    }
    }
}