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
What is a methodologist?
What are the practical benefits, if any, of importing a specific class rather than an entire package (e.g. Import java.net.* Versus import java.net.socket)?
What is meant by data hiding/encapsulation?
What are basic keywords?
What is java argument list?
What is heterogeneous in java?
Implement a stack with push (), pop() and min() in O(1) time.
Can you pass functions in java?
what is thread? What are the high-level thread states? Or what are the states associated in the thread? : Java thread
What is skeleton and stub?
Can java run on google chrome?
What is an example of a constant variable?
Is main a function?
What is a module function?
Is array primitive data type in java?