0 votes

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

in Engine by (322 points)

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.