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
Which is better arraylist or vector?
What is the difference between processes and threads?
Can we have a method name same as class name in java?
What is a stringbuilder?
what is recursion in java
Why wait(),notify(),notifyAll() methods defined in Object class althought we are using in only threads.
give an example for encapsulation?
What is a flag variable?
How do I get a substring?
Explain different ways of creating a thread?
What is your platform’s default character encoding?
How would you format a date in java? I.e. In the ddmmyyy format?
Which api is provided by java for operations on set of objects?
Can the garbage collection be forced by any means?
Can we write any code after throw statement?