How to Sort list of Strings in ascending order without using
java api.
Answer Posted / megha
package practise;
import java.util.Scanner;
class Sorting {
public static void main(String[] input)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the count :\t ");
int k=sc.nextInt();
String temp=new String();
String names[]=new String[k];
System.out.println("enter the names");
for(int i=0;i<k;i++)
{
names[i] = sc.next();
}
for( int i=0;i<k;i++)
{
for(int j =i+1;j<k;j++)
{
if(names[i].compareTo(names[j])>0)
{
temp = names[i];
names[i] = names[j];
names[j] = temp;
}
}
}
for(String name:names)
{
System.out.println(name);
}
}
}
Is This Answer Correct ? | 25 Yes | 0 No |
Post New Answer View All Answers
How do you convert int to char in java?
What is udp in java?
What is difference between stringbuffer and string?
What is the different between get and post?
How will you initialize an Applet?
What data type is a string?
What is the difference between final, finally and finalize() in java?
How does java enable high performance?
What a static class can contains?
What do you mean by platform independence of Java?
What is private protected in java?
What do you understand by garbage collection in Java? Can it be forced to run?
Explain different forms of polymorphism?
Which method returns the length of a string?
How can we achieve thread safety in java?