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 domain object

1 Answers  


Which containers use a flowlayout as their default layout in java programming?

0 Answers  


Ducking is automatic in checked exception?True or false

2 Answers  


What is the difference between super class & sub class?

0 Answers  


Why we used break and continue statement in java?

0 Answers  


What is the public field modifier?

0 Answers  


take an array with -ve and +ve value both.find out the nearest value of 0(zero).if two values are same like(-2 and +2)then extract +2 is nearest of 0(zero).

0 Answers  


What does %4d mean in java?

0 Answers  


How do you compare characters in java?

0 Answers  


What are controls and their different types in awt?

0 Answers  


Hi friends, i am new to java. can you explain how java is secured.

2 Answers  


Can a class inherit the constructors of its superclass?

9 Answers   iFlex, Wipro,


Categories