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

What is null mean in java?

627


how we can make a read-only class in java?

546


What is the file extension for java?

616


What is contract between hashcode and equal method?

576


How to perform linear search in java?

570






What is Mutex (Mutual Exclusion Object) ?

629


What is return in java?

560


What is the use of static class?

549


What is the use of static methods?

593


What do the thread?class methods run() and start() do?

547


Is array synchronized in java?

557


Where are register variables stored?

595


What is a modifier?

878


Are arrays primitive data types?

642


Does variable declaration allocate memory?

589