| 5 | import java.util.List; |
| 6 | |
| 7 | public class Sorter implements Comparator<Item> { |
| 8 | |
| 9 | private final String type; |
| 10 | private final String order; |
| 11 | |
| 12 | public static void sort(String type, String order, List<Item> items) { |
| 13 | Collections.sort(items, new Sorter(type, order)); |
| 14 | } |
| 15 | |
| 16 | public Sorter(String type, String order) { |
| 17 | this.type = type; |
| 18 | this.order = order; |
| 19 | } |
| 20 | |
| 21 | @Override |
| 22 | public int compare(Item o1, Item o2) { |
| 23 | boolean asc = order.equals("asc"); |
| 24 | switch (type) { |
| 25 | case "name": |
| 26 | return asc ? o1.getName().compareTo(o2.getName()) : o2.getName().compareTo(o1.getName()); |
| 27 | case "size": |
| 28 | return asc ? Long.compare(o1.getSize(), o2.getSize()) : Long.compare(o2.getSize(), o1.getSize()); |
| 29 | case "date": |
| 30 | return asc ? o1.getDate().compareTo(o2.getDate()) : o2.getDate().compareTo(o1.getDate()); |
| 31 | default: |
| 32 | return -1; |
| 33 | } |
| 34 | } |
| 35 | } |
nothing calls this directly
no outgoing calls
no test coverage detected