Alex G Alex G - 7 months ago 8
Java Question

Lesson 19 Project - Two Orders For Price of One - Sorting Arrays



I am using the Blue Pelican Java textbook and am stuck on the project for Lesson 19. It asks to:

Modify the code below to print two side-by-side columns. The first column should be in ascending order (like the code below will print), and the second column should be in descending order. The output should be:

Ascend   Descend
Agnes Thomas
Alfred Mary
Alvin Lee
Bernard Herman
Bill Ezra
Ezra Bill
Herman Bernard
Lee Alvin
Mary Alfred
Thomas Agnes


The code currently is:

import java.util.*;
public class Tester
{
public static void main(String args[])
{
String ss[] = {"Bill", "Mary", "Lee", "Agnes", "Alfred", "Thomas", "Alvin", "Bernard", "Ezra", "Herman"};
Arrays.sort(ss);
for(String varSs: ss)
System.out.println(varSs);
}
}


How do I make the Descending column?

Thanks, -AJ

Answer

You could try:

Arrays.sort(ss, Collections.reverseOrder());

import java.util.Collections which you've already imported includes the reverseOrder method which will sort in descending order.

Comments