effect icon indicating copy to clipboard operation
effect copied to clipboard

Make Array.sort Order arg optional to match js implementation.

Open jessekelly881 opened this issue 1 year ago • 3 comments

What is the problem this feature would solve?

Array.prototype.sort's compare function is optional and it automatically handles sorting number[], string[], etc. I think we should do the same with Array.sort(et al) making the Order arg optional and relying on the default whenever possible. Array.sort() is more convenient and would be more performant than Array.sort(Order.number) or Array.sort(Order.string)

What is the feature you are proposing to solve the problem?

^

What alternatives have you considered?

No response

jessekelly881 avatar Apr 23 '24 08:04 jessekelly881

Array.sort is mutable so it can't be used directly, we'd have to clone the array leading to the same perf tradeoff

mikearnaldi avatar Apr 23 '24 12:04 mikearnaldi

The mutability of .sort isn't relevant here though. I'm only suggesting that we change Array.sort(o: Order) to Array.sort(o?: Order) without changing the implementation. A compare fn isn't required when using either Array.prototype.sort or Array.prototype.toSorted unless you want to override the default behavior.

["b","a","c"].sort(Order.string) // ["a", "b", "c"]
["b","a","c"].sort() // ["a", "b", "c"]

jessekelly881 avatar Apr 23 '24 16:04 jessekelly881

would be more performant than Array.sort(Order.number) or Array.sort(Order.string)

was answering to:

would be more performant than Array.sort(Order.number) or Array.sort(Order.string)

mikearnaldi avatar Apr 24 '24 04:04 mikearnaldi