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 difference between equals and ==?
Can a class be defined inside an interface?
Variables used in a switch statement can be used with which datatypes?
Can we have a try block without catch block?
How are the elements of a gridbaglayout organized in java programming?
What is a lambda expression ? What's its use ?
What is volatile data type?
How big is a 64 bit float?
How many arguments can a method have java?
What is the difference between length and length() method in java?
What does snprintf return?
Why to use nested classes in java? (Or) what is the purpose of nested class in java?
how many types of Inheritance?
Which sorting algorithm is best in java?
Give differences between Quicksort &Mergesort. When should these sorts be used andwhat is their running time in java?