How to Sort Strings which are given in List and display in
ascending order without using java api.
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 |
Which is bigger float or double java?
INTERVIEW QUESTION FOR ANDROID
What is the method used to get the absolute value of a number?
What is an array in java?
How do you use wildcards?
Can we cast any other type to boolean type with type casting?
Explain the usage of this with constructors?
Urgent requirement of JAVA/.NET/QA in a CMMI level3 MNC for Pune location.
Why is java multithreaded?
When does an object becomes eligible for garbage collection in java?
what is the difference between a java object reference and c++ pointer?
String is an immutable object. Then how can the following code be justified. String s1 = ?ABC?; String s1 = s1+?XYZ?; s.o.p(s1); The output is ABCXYZ, which is the value of s1 ?
6 Answers Flextronics, Keane India Ltd,