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


Please Help Members By Posting Answers For Below Questions

How do you convert int to char in java?

580


What is udp in java?

537


What is difference between stringbuffer and string?

500


What is the different between get and post?

506


How will you initialize an Applet?

625






What data type is a string?

515


What is the difference between final, finally and finalize() in java?

523


How does java enable high performance?

697


What a static class can contains?

703


What do you mean by platform independence of Java?

538


What is private protected in java?

555


What do you understand by garbage collection in Java? Can it be forced to run?

558


Explain different forms of polymorphism?

661


Which method returns the length of a string?

573


How can we achieve thread safety in java?

691