How to fix this custom sorting debugger error: "unguarded_linear_insert"

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By acwr

I have this simple custom sorter:

class Sorter:
static func sort_descending(a, b):
	if a >= b:
		return true
	return false

Which throws this error:

E 0:00:31.927   unguarded_linear_insert: bad comparison function; sorting will be broken 
<C++ Source>  ./core/sort_array.h:263 @ unguarded_linear_insert()

Funny enough this example was derived from the Godot docs:

class MyCustomSorter:
static func sort_ascending(a, b):
    if a[0] < b[0]:
        return true
    return false

What’s worse than just the error being annoying is I don’t know if the sorting is actually “being broken” like the error says it is, as the comparison values are dynamic and affected by RNG.

:bust_in_silhouette: Reply From: Inces

Did You try it without “or equal” part ?

I did, I actually added that thinking it may help, but it did not.

acwr | 2021-03-19 17:16

Can You show what are You trying to sort ?

Inces | 2021-03-19 18:12

Its just an array of numbers with an int of 1-8 determined by rng added to them.

Essentially:

a + rng.randi_range(1, 8)

acwr | 2021-03-19 18:17

Ok, I have never seen this error, i won’t wrap my head around it.
But do You really need to custom sort an array of integers only for the reason of being descending ? If You need those values samowhere You can just sort() it normally and get them in reverse or using pop_back()

Inces | 2021-03-19 18:25

My use-case involves adding the RNG value inside the sorter, though I have the error with or without that so I didn’t include it in the original snippet. It’s creating a turn order for units in battle using their speed value + RNG: i.e. a = a + rng(8), b = b + rng(8).

acwr | 2021-03-19 19:05