String Sorting
This java code simple to sort the java string array . For this we use swapping concept
and comparing two string we sort the string .
The method takes a string array and reverse it and print it.
class SortingStrinArray {
public static void sortString(String[] s ) {
String temp=null;
int l=s.length;
int i,j;
System.out.println("the strings before are:");
for(i=0; i< l;i++)
System.out.println(" "+s[i]);
for(i=0; i< l;i++)
{
for(j=0;j< l;j++)
{
if(s[j].compareTo(s[i])<0) {
temp=s[i];
s[i]=s[j];
s[j]=temp;
}
System.out.println(" ");
System.out.println("the array elements after sorting are:");
for(i=0;i< l;i++)
System.out.print(" "+s[i]);
}
}
}
public static void main(String args[])
{
try {
String st[]={"banglore","delhi","madras","calcutta","pune"};
sortString(st);
}catch (Exception e){
}
}
}
Out put of the code will be a given array string and sorted string array.
the strings before are:
banglore
delhi
madras
calcutta
pune
the array elements after sorting are:
banglore delhi madras calcutta pune
String Sorting
Reviewed by Mukesh Jha
on
10:54 PM
Rating:
No comments:
Add your comment