May I want an intuitive array sorting function.

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

like this:

.sort ( [property], reverse: boolean = false )

I hope this function can be as simple as sorting a list by name or date on any website. But now you have to write additional functions to achieve it. It’s hard to understand and implement it.

:bust_in_silhouette: Reply From: zhyrin

You didn’t pose a question.
What difficulty did you encounter?
What do you want the outcome to be?
Do you want to sort an array on a custom criteria (not simply ascending)?
If that is what you mean, there is Array.sort_custom().

func do_work():
    var array = [...]
    array.sort_custom(self, "_my_sorting_function")

func _my_sorting_function(left, right):
    if <logic for when left should be before right>:
        return true
    return false

For each criteria you want to be able to sort you write a custom sorting function.