MCPcopy Index your code
hub / github.com/ccxt/ccxt / compareJsLike

Method compareJsLike

java/lib/src/main/java/io/github/ccxt/base/Generic.java:86–98  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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();

Callers 1

sortByMethod · 0.95

Calls 2

toDoubleOrNullMethod · 0.95
equalsMethod · 0.45

Tested by

no test coverage detected