Thraka / SadConsole

A .NET ascii/ansi console engine written in C# for MonoGame and XNA. Create your own text roguelike (or other) games!
MIT License
1.23k stars 120 forks source link

Add support for sorting `IScreenObject` in the parents collection #310

Closed Thraka closed 1 year ago

Thraka commented 1 year ago

Sort would have to be a manual operation. Automatic sort shouldn't be supported, as it may change the entire behavior of a collection of objects. The user has full control to either keep the order as they added the children, or invoke a sort.

Chris3606 commented 1 year ago

Quick thought on this; there is a significant asymptotic notation difference in the algorithms required to insert an element at an an appropriate position to keep an already-sorted list sorted, vs sorting an unsorted list. Specifically, finding the position of an item and inserting it, is worst-case O(n), whereas sorting an unsorted list is O(n*log(n)).

Therefore, if the API only supports the user inserting an item then calling a sort function as the method to change the order, this could limit a user from being able to write the most efficient code.

Perhaps the API could also support inserting an item at a specified index in the children list? That would allow a user to implement the "find the proper index and insert" method of maintaining sorting.