(String box1, String box2)
| 7 | public static List<String> sortBoxes(List<String> boxList) { |
| 8 | Comparator<String> comparator = new Comparator<String>() { |
| 9 | @Override |
| 10 | public int compare(String box1, String box2) { |
| 11 | // split each junction box into two parts identifier, content |
| 12 | String[] split1 = box1.split(" ", 2); |
| 13 | String[] split2 = box2.split(" ", 2); |
| 14 | |
| 15 | boolean isDigit1 = Character.isDigit(split1[1].charAt(0)); |
| 16 | boolean isDigit2 = Character.isDigit(split2[1].charAt(0)); |
| 17 | |
| 18 | // case 1). both boxes are letter-boxes |
| 19 | if (!isDigit1 && !isDigit2) { |
| 20 | // first compare the content |
| 21 | int cmp = split1[1].compareTo(split2[1]); |
| 22 | if (cmp != 0) |
| 23 | return cmp; |
| 24 | // boxes of same content, compare the identifiers |
| 25 | return split1[0].compareTo(split2[0]); |
| 26 | } |
| 27 | // case 2). one of boxes is digit |
| 28 | if (!isDigit1 && isDigit2) |
| 29 | // the letter box comes before digit-boxes |
| 30 | return -1; |
| 31 | else if (isDigit1 && !isDigit2) |
| 32 | return 1; |
| 33 | else |
| 34 | // case 3). both boxes are digit |
| 35 | return 0; |
| 36 | } |
| 37 | }; |
| 38 | |
| 39 | Collections.sort(boxList, comparator); |
nothing calls this directly
no outgoing calls
no test coverage detected