How to Sort Strings which are given in List and display in
ascending order without using java api.

Answer Posted / kumar

public class sorting_strings
{
static void sortString()
{
String a[]={"n","g","a","i","h","v","q"};

for (int i = 0; i < a.length; i++)
{
for (int j = 0; j < a.length; j++) {
int num=a[i].compareTo(a[j]);
//System.out.println("num="+num);
if(num>=0)
{
String temp=a[i];
a[i]=a[j];
a[j]=temp;

}

if(num<0)
{
//String temp=a[i];
a[i]=a[i];
a[j]=a[j];

}
}
}
//display
int x=a.length;
for (int i =(x-1); i>=0 ; i--) {
System.out.print(a[i]+" ");
}
}
public static void main(String[] args) {

sortString();
}

}

Is This Answer Correct ?    3 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Differentiate between a constructor and a method? Can we mark constructors final?

970


How do you convert int to char in java?

843


Are arrays passed by reference in java?

702


What is the purpose of assert keyword used in jdk1.4.x?

777


What happens if we don’t define serial version uid?

805


What is byte value?

767


What is main method?

790


What is meant by class and object in java?

745


Can we able to pass objects as an arguments in java?

801


What is the driver class?

778


What is object of class in java?

863


What is the purpose of extern variable?

774


What is complexity and its types?

745


What is JFC?

918


Can we define static methods inside interface?

747