JS-like comparison for sortBy. Mirrors TS `<`/`>` semantics: - both numeric (Number, or Strings that parse as numbers): numeric compare - both non-numeric strings: lexicographic - mixed: fall back to lex compare on toString() (matches JS coercion rules closely enough for ccxt's sort use-cases)
(Object a, Object b, Object defaultValue)
| 84 | * currency-merge path started feeding mixed-type values into the same sort. |
| 85 | */ |
| 86 | private static int compareJsLike(Object a, Object b, Object defaultValue) { |
| 87 | if (a == null) a = defaultValue; |
| 88 | if (b == null) b = defaultValue; |
| 89 | boolean aEmpty = (a == null) || "".equals(a); |
| 90 | boolean bEmpty = (b == null) || "".equals(b); |
| 91 | if (aEmpty && bEmpty) return 0; |
| 92 | if (aEmpty) return -1; |
| 93 | if (bEmpty) return 1; |
| 94 | Double da = toDoubleOrNull(a); |
| 95 | Double db = toDoubleOrNull(b); |
| 96 | if (da != null && db != null) return Double.compare(da, db); |
| 97 | return String.valueOf(a).compareTo(String.valueOf(b)); |
| 98 | } |
| 99 | |
| 100 | private static Double toDoubleOrNull(Object o) { |
| 101 | if (o instanceof Number n) return n.doubleValue(); |
no test coverage detected