While sorting an array ascending, how can I randomize equal values order?

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

Hi!
Say I have the next array: [[2, a], [1, b], [3, c], [2, d]]
From what I have tested [2, a] will be always first than [2, d] using the Array.sort_custom() method as:

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

So I tried to do:

static func custom_sort(a,b):
if a[0] < b[0]:
	return true
elif a[0] == b[0]:
	var rand = round(randf())
	if rand == 1:
		return true
return false

But, besides it works, it throws me the next error sometimes:

unguarded_linear_insert: bad comparison function; sorting will be broken

Then, I solved it partially with doing a loop comparing one value to the next and relocate it randomly if them are equal. But it’s true randomness only when there are only 2 equal values, and there could be many more.

How do would you solve it?
Thanks