From 38e3bbdd2b2fc095d727cb74cc7cd2cbe467f3f5 Mon Sep 17 00:00:00 2001 From: Pieter12345 Date: Sun, 17 Dec 2023 05:14:20 +0100 Subject: [PATCH] Support integer return type in array_sort closure --- .../laytonsmith/core/functions/ArrayHandling.java | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/laytonsmith/core/functions/ArrayHandling.java b/src/main/java/com/laytonsmith/core/functions/ArrayHandling.java index 86f33632f..9a3f375c1 100644 --- a/src/main/java/com/laytonsmith/core/functions/ArrayHandling.java +++ b/src/main/java/com/laytonsmith/core/functions/ArrayHandling.java @@ -1828,9 +1828,12 @@ private CArray merge(CArray left, CArray right, CClosure closure, Target t) { } else { value = -1; } + } else if(c.isInstanceOf(CInt.TYPE)) { + long longVal = ((CInt) c).getInt(); + value = (longVal > 0 ? 1 : (longVal < 0 ? -1 : 0)); } else { throw new CRECastException("The custom closure did not return a value (or returned an invalid" - + " type). It must always return true, false, or null.", t); + + " type). It must always return true, false, null, or an integer.", t); } if(value <= 0) { result.push(left.get(0, t), t); @@ -1882,10 +1885,10 @@ public String docs() { + " operation. Due to this, it has slightly different behavior than array_normalize, which could" + " have also been implemented in place.\n\nIf the sortType is a closure, it will perform a" + " custom sort type, and the array may contain any values, including sub array values. The closure" - + " should accept two values, @left and @right, and should return true if the left value is larger" - + " than the right, and false if the left value is smaller than the right, and null if they are" - + " equal. The array will then be re-ordered using a merge sort, using your custom comparator to" - + " determine the sort order."; + + " should accept two values, @left and @right, and should return true or a positive integer if the" + + " left value is larger than the right, and false or a negative integer if the left value is" + + " smaller than the right, and null or 0 if they are equal. The array will then be re-ordered" + + " using a merge sort, using your custom comparator to determine the sort order."; } @Override