Skip to content

Commit b3b19f9

Browse files
committed
Sort ^() throws ClassCastException with mixed Integer/Double values #97
1 parent 3168b6a commit b3b19f9

3 files changed

Lines changed: 21 additions & 1 deletion

File tree

src/main/java/com/dashjoin/jsonata/Jsonata.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1391,7 +1391,7 @@ else if (aa.getClass().isAssignableFrom(bb.getClass()) ||
13911391
if(aa.equals(bb)) {
13921392
// both the same - move on to next term
13931393
continue;
1394-
} else if (((Comparable)aa).compareTo(bb)<0) {
1394+
} else if (Utils.compareTo(aa, bb)<0) {
13951395
comp = -1;
13961396
} else {
13971397
comp = 1;

src/main/java/com/dashjoin/jsonata/Utils.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,4 +275,16 @@ static Map mapOf(String key, Object value) {
275275
map.put(key, value);
276276
return map;
277277
}
278+
279+
/**
280+
* called from Jsonata.compare() - aa and bb are either both strings or numbers
281+
* for numbers, compare doubleValue to avoid cast error when int is compared to double
282+
*/
283+
@SuppressWarnings("unchecked")
284+
static int compareTo(Object aa, Object bb) {
285+
if (aa instanceof String)
286+
return ((Comparable)aa).compareTo(bb);
287+
else
288+
return Double.compare(((Number)aa).doubleValue(), ((Number)bb).doubleValue());
289+
}
278290
}

src/test/java/com/dashjoin/jsonata/ArrayTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,12 @@ public void testWildcardFilter() {
7575
var expression2 = jsonata("**[value.Product = 'Product1']");
7676
Assertions.assertEquals(value1, expression2.evaluate(data));
7777
}
78+
79+
@Test
80+
public void testSortTypes() {
81+
var e = jsonata("[{'value': 5.9}, {'value': 8}] ^(<value).value");
82+
Assertions.assertEquals(List.of(5.9, 8), e.evaluate(null));
83+
e = jsonata("[{'value': 'b'}, {'value': 'a'}] ^(<value).value");
84+
Assertions.assertEquals(List.of("a", "b"), e.evaluate(null));
85+
}
7886
}

0 commit comments

Comments
 (0)