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



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

Answer / 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

More Core Java Interview Questions

What is the difference between stored procedure & function?

0 Answers  


what is difference between set and list in collection?

3 Answers   Satyam,


Why java is used everywhere?

0 Answers  


How do you empty a list in java?

0 Answers  


What is the difference between Object and Instance?

5 Answers   TCS,






Explain importance of finally block in java?

0 Answers  


In C we use only compiler. Why java uses both compiler and interpreter? What is its significance?

5 Answers  


What is mvc in java?

0 Answers  


What is lastindexof in java?

0 Answers  


Can I create any Marker Interface? If yes then how can I use it???

4 Answers  


java is pure object-oriented programming language?why it is platform independent.

1 Answers  


Can an arraylist be empty?

0 Answers  


Categories