winglang / wing

A programming language for the cloud ☁️ A unified programming model, combining infrastructure and runtime code into one language ⚡
https://winglang.io
Other
5.06k stars 198 forks source link

Allow sorting with Arrays #7088

Closed boyney123 closed 2 months ago

boyney123 commented 2 months ago

Use Case

I have some array, it would be nice to be able to sort it.

let x = [1, 10, 5];

// no sort method
x.sort();

Proposed Solution

let x = [1, 10, 5];
x.sort(); // 1, 5, 10

Also allow a function as a param?
x.sort(sortFunc); 

Implementation Notes

No response

Component

No response

Community Notes

skyrpex commented 2 months ago

Given the immutability nature that we're trying to achieve in Wing, maybe it'd make more sense if the array sort method returned a new sorted array:

let unsorted = [1, 10, 5];

let sorted = unsorted.sort();

Allow passing a compare function is a must, too 👍🏻. Not sure about the API for Array and MutArray, though... unsorted.sort() and unsorted.sortMut()?

Chriscbr commented 2 months ago

Perhaps something like array.sort() for sorting in-place and array.sorted() for returning a shallow copy that has been sorted? (sorting in-place would only be allowed on MutArray)

Chriscbr commented 2 months ago

Closing as duplicate of https://github.com/winglang/wing/issues/4998