Skip to content

Commit e0f4624

Browse files
committed
Improve documentation/error on Array.sort_custom() function comparison
1 parent 1f56d96 commit e0f4624

File tree

2 files changed

+3
-1
lines changed

2 files changed

+3
-1
lines changed

core/templates/sort_array.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

3636
#define ERR_BAD_COMPARE(cond) \
3737
if (unlikely(cond)) { \
38-
ERR_PRINT("bad comparison function; sorting will be broken"); \
38+
ERR_PRINT("Bad comparison function (the function returns `true` for values that are equal to each other). This results in broken sorting. To avoid this, use `<` or `>` for comparisons instead of `<=` or `>=`."); \
3939
break; \
4040
}
4141

doc/classes/Array.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@
289289
<description>
290290
Returns the index of [param value] in the sorted array. If it cannot be found, returns where [param value] should be inserted to keep the array sorted (using [param func] for the comparisons). The algorithm used is [url=https://en.wikipedia.org/wiki/Binary_search_algorithm]binary search[/url].
291291
Similar to [method sort_custom], [param func] is called as many times as necessary, receiving one array element and [param value] as arguments. The function should return [code]true[/code] if the array element should be [i]behind[/i] [param value], otherwise it should return [code]false[/code].
292+
If the order of the two elements does not matter (e.g. they are equal), it should return [code]false[/code]. This means that for comparisons, [code]&lt;[/code] or [code]&gt;[/code] should always be used instead of [code]&lt;=[/code] or [code]&gt;=[/code]. If this rule is not followed, an error is printed as sorting will be broken.
292293
If [param before] is [code]true[/code] (as by default), the returned index comes before all existing elements equal to [param value] in the array.
293294
[codeblock]
294295
func sort_by_amount(a, b):
@@ -769,6 +770,7 @@
769770
<description>
770771
Sorts the array using a custom [Callable].
771772
[param func] is called as many times as necessary, receiving two array elements as arguments. The function should return [code]true[/code] if the first element should be moved [i]before[/i] the second one, otherwise it should return [code]false[/code].
773+
If the order of the two elements does not matter (e.g. they are equal), it should return [code]false[/code]. This means that for comparisons, [code]&lt;[/code] or [code]&gt;[/code] should always be used instead of [code]&lt;=[/code] or [code]&gt;=[/code]. If this rule is not followed, an error is printed as sorting will be broken.
772774
[codeblock]
773775
func sort_ascending(a, b):
774776
if a[1] &lt; b[1]:

0 commit comments

Comments
 (0)