(Object array, Object value1, Object desc2, Object defaultValue2)
| 44 | // ---------- sortBy ---------- |
| 45 | |
| 46 | public static List<Object> sortBy(Object array, Object value1, Object desc2, Object defaultValue2) { |
| 47 | boolean desc = (desc2 instanceof Boolean b) ? b : false; |
| 48 | Object defaultValue = (defaultValue2 != null) ? defaultValue2 : ""; |
| 49 | List<Object> lst = (List<Object>) array; |
| 50 | |
| 51 | List<Object> sorted; |
| 52 | if (value1 instanceof String key) { |
| 53 | sorted = new ArrayList<>(lst); |
| 54 | sorted.sort((a, b1) -> compareJsLike( |
| 55 | ((Map<String, Object>) a).get(key), |
| 56 | ((Map<String, Object>) b1).get(key), |
| 57 | defaultValue)); |
| 58 | } else { |
| 59 | int index = ((Number) value1).intValue(); |
| 60 | sorted = new ArrayList<>(lst); |
| 61 | sorted.sort((a, b1) -> { |
| 62 | Object va = (a instanceof List<?> la && index >= 0 && index < la.size()) ? la.get(index) : null; |
| 63 | Object vb = (b1 instanceof List<?> lb && index >= 0 && index < lb.size()) ? lb.get(index) : null; |
| 64 | return compareJsLike(va, vb, defaultValue); |
| 65 | }); |
| 66 | } |
| 67 | if (desc) Collections.reverse(sorted); |
| 68 | return sorted; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * JS-like comparison for sortBy. Mirrors TS `<`/`>` semantics: |
no test coverage detected