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

How to create packages in java?

722


How can I right-justify a string?

854


Explain access specifiers?

906


What is the disadvantage of synchronization?

772


How do you check if a string is lexicographically in java?

720


What are three advantages of using functions?

756


What is the point of java?

778


What environment variables are required to be set on a machine in order to run Java programs?

870


What is sortedmap interface?

772


Can we execute java program without main method?

735


When should I use singleton pattern?

808


What is a prefix function.write down a code to compute prefix function.

768


Can inner class have constructor?

750


What are the methods to rectify ambiguities in the interfaces in JAVA?

830


Can we have return statement in finally clause? What will happen?

744