How to write a program in Java to sort a given list of names in ascending order?

Answer:
// Names are stored here
final ArrayList<String> names = new ArrayList<String>();

// Get input from standard in
final BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

// Prompt for input
System.out.println("Enter names to sort: ");

// Get input
String currentLine;
while ((currentLine = in.readLine()) != null) {

// A blank line equals the end of input
if (currentLine.equals("")) {
break;
}
names.add(currentLine);

}

// Remember to close our input
in.close();


System.out.println("Sorting: " + names);

// Let Java do our sorting for us.
Collections.sort(names);

System.out.println("Sorted: " + names);

Note: There are comments associated with this question. See the discussion page to add to the conversation.
First answer by ID0995895589. Last edit by Moobler. Contributor trust: 354 [recommend contributor recommended]. Question popularity: 4 [recommend question].