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
why would you use a synchronized block vs. Synchronized method? : Java thread
Can we have a try block without catch block?
Can you use abstract and final both with a method?
What do you understand by final value?
What is a data structure java?
Difference between linkedlist and arraylist.
What is the difference between compiler and jvm?
I want to re-reach and use an object once it has been garbage collected. How it's possible?
How to print nodes of a Binary tree?
what do you mean by marker interface in java?
Which container method is used to cause a container to be laid out and redisplayed in java programming?
Is array passed by reference in java?
What are data types in oop?
What is difference between static class and singleton pattern?
What is flag in python?