Skip to content

Instantly share code, notes, and snippets.

@akarnokd
Created June 19, 2022 09:00
Show Gist options
  • Save akarnokd/ac0bc3f62b6aec9e649624b5a3a3aa5a to your computer and use it in GitHub Desktop.
Save akarnokd/ac0bc3f62b6aec9e649624b5a3a3aa5a to your computer and use it in GitHub Desktop.
import java.util.*;
public class SortySort {
public static String stringSorter(List<String> stringList, String sortingOrder) {
if ((stringList == null) || (stringList.isEmpty())) {
return "Given stringList or sortingOrder is empty, null or blank space";
}
if ((stringList.size() == 1)) {
return "The list contains an empty or blank space value";
}
if (sortingOrder.equals("asc")) {
Collections.sort(stringList);
return stringList.toString();
}
if (sortingOrder.equals("desc")) {
Collections.sort(stringList, Collections.reverseOrder());
return stringList.toString();
}
return " ";
}
public static void main(String[] args) {
List<String> list = new ArrayList<>();
Scanner scan = new Scanner(System.in);
while (scan.hasNext()) {
if (list.size() <= 2) {
String k = scan.nextLine();
list.add(k);
} else {
break;
}
}
String order = scan.next();
System.out.println(stringSorter(list, order));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment