-
-
Notifications
You must be signed in to change notification settings - Fork 23.5k
Add sorted() method to Array and Dictionary
#112138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
3efbc68 to
a101709
Compare
sorted() method to Array and Dictionary
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd personally suggest adding Array.sorted_custom as well for completeness
|
I must note that the reason these methods don't exist is probably due to performance concerns, to prevent users from shooting themselves in the foot with a wasteful array copy. I just checked to confirm my doubts in the proposal, and yeah. I don't think adding these for the packed arrays is a great idea, personally. |
|
@Mickeon Yes I agree the performance is worse than if you are not already duplicating the container. In this case I intentionally made it so that all instances of After reading some of the proposal you linked, I think that this is a bit different than what is proposed there. If |
|
You're right. In the proposal it is rightfully assumed that there would be no performant alternative to Still, let's tread carefully here. We generally avoid bloating the API, especially for core Variant types, unless there's big demand for it. Regardless of what you do, a note warning of the potential performance caveats should be added (similarly to what is done in |
|
I think the benefit of saving one line needs to be weighed against the risk of abuse, especially with the potential confusion from things like Having to use: var my_copy = my_array.duplicate()
my_copy.sort()Isn't that big of an inconvenience I'd say, and I don't think there would be any real performance difference between that and this new method The special case I can see is cases like immediate use, like: for key in my_dictionary.keys().sorted():Where you don't ever need to touch the copy "really" Personally I think the safest option, though it's unfortunately more disruptive, would be to have Edit: I think there are three options with varying pros and cons:
|
|
While similar to #112077, as well as godotengine/godot-proposals#5771 and godotengine/godot-proposals#6877, it's not quite the same. Personally, I think it is less error prone than godotengine/godot-proposals#5771 and godotengine/godot-proposals#6877, so I think it has a better chance of being accepted. We'll monitor discussions and interest in this feature, and will consider merging it if it becomes popular enough. |
a101709 to
a1cb590
Compare
a1cb590 to
4467557
Compare
Similar to #112077 but for
sort()instead ofreverse(). This adds the method toArray, thePacked*Arrays, andDictionary. It also addssorted_custom()toArrayHaskell has left me feeling like these are missing every time I go to use the ones we already have and remember that they don't return anything.
Closes godotengine/godot-proposals#13509.