Skip to content
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

Array.sort() method support #4998

Open
itssubhodiproy opened this issue Nov 18, 2023 · 11 comments
Open

Array.sort() method support #4998

itssubhodiproy opened this issue Nov 18, 2023 · 11 comments
Labels
✨ enhancement New feature or request needs-discussion Further discussion is needed prior to impl 🎨 sdk SDK

Comments

@itssubhodiproy
Copy link
Contributor

itssubhodiproy commented Nov 18, 2023

Feature Spec

let arr: MutArray<num>=[2, 1, 3, 9, 6, 4];
arr.sort();
log("${arr}");  // it should print sorted array in ascending order, eg: [1, 2, 3, 4, 6, 9]

Component

Wing SDK

Community Notes

  • Please vote by adding a 👍 reaction to the issue to help us prioritize.
  • If you are interested to work on this issue, please leave a comment.
  • If this issue is labeled needs-discussion, it means the spec has not been finalized yet. Please reach out on the #dev channel in the Wing Slack.
@itssubhodiproy itssubhodiproy added needs-discussion Further discussion is needed prior to impl ✨ enhancement New feature or request labels Nov 18, 2023
@staycoolcall911 staycoolcall911 added 🎨 sdk SDK good first issue Good for newcomers labels Nov 19, 2023
@revitalbarletz revitalbarletz self-assigned this Nov 26, 2023
@revitalbarletz
Copy link
Contributor

Issue taken by Lauty Suarez!

@Chriscbr
Copy link
Contributor

Chriscbr commented Dec 1, 2023

Sorting is certainly missing! thanks for opening this issue @subh-cs. I noticed a small bug in the issue -- the array you gave was immutable, so it shouldn't be sortable. But of course, sort() makes sense as a capability for MutArrays. I fixed it for your convenience :-)

For implementing this issue I think it would be forward thinking to define a semantics for the sorting. Even if we're using JavaScript's default sort implementation under the hood, we should note the essential edge cases in the docs to ensure the method works as people expect when they're coming from non-JS languages.

For example, if an array contains a collection of structs or class objects, how are the values sorted (supposing no comparison function is provided by the user)? We should include a note in the docstring how this edge case is handled.

I think it would also be handy to document whether or not the sort implementation is stable or unstable since it's a detail relevant for implementing many algorithms.

@ekeren
Copy link
Collaborator

ekeren commented Dec 3, 2023

@Chriscbr, there are two options of sort that I can think of:

// Option 1: Mutating  - relevant only for MutableArray
arr.sort();
// arr is not sorted 

// Option 2: Non-Mutating - relevant for Array and MutArray
let sorted = arr.sort();
// arr state remains the same 

Did we consider option 2?

@garysassano
Copy link
Collaborator

What about reverse()? Should that be a separate method or part of sort() with an optional parameter?

@Chriscbr
Copy link
Contributor

Chriscbr commented Dec 3, 2023

@ekeren I'd name the non-mutating version sorted() and the mutating version sort() - I think we can support both.

@garysassano reverse() as a convenience method sounds like a good idea to me.

@revitalbarletz
Copy link
Contributor

@subh-cs Are you looking into this one?

@Warkanlock
Copy link
Collaborator

I'm working on this one

@staycoolcall911
Copy link
Contributor

Sounds good, then I'll assign you

@Warkanlock
Copy link
Collaborator

Warkanlock commented Feb 2, 2024

I have a few comments regarding this change:

  1. Lack of a comparator function (which might be ideal, and we can iterate on top of that)
  2. A comparator function would imply the creation of dynamic typing, and, therefore, we would run into the same problems I encountered when I tried to implement map.entries() #5005.

I'd love to discuss this with a more experienced dev of winglang since this will require more changes than the ones involved in the SDK

@staycoolcall911
Copy link
Contributor

Got you @Warkanlock. We'll get you someone to help. Wrote to you over your post on Slack dev channel.

mergify bot pushed a commit that referenced this issue Feb 11, 2024
…n the sdk (#5631)

The purpose of this PR was to unblock more safe implementations of #4998 and #5005

To that end, the following changes were needed

- `@typeparam` annotation can now be used on interfaces in TS
- Generics methods can now take/return other generic classes/interfaces
- Generics methods can now take/return `[]`s of generic classes/interfaces
- `@callable` can be added to an interface in TS to turn the type into a function, where the first (and only) member of the interface will be used as the type signature

All means you could define something like this in TS

```ts
/**
* @typeparam T1
* @callable
*/
export interface IComparer {
  fn(x: T1, y: T1): num;
}

/**
* @typeparam T1
*/
class Array {
  sort(sorter: IComparer) { ... }
}
```

and then use in wing like this

```wing
[1, 2, 3].sort((x, y) => { ... })
```

Note on testing: I have no idea how to test the generic stuff without implementing the features. I partially implemented them to test this PR out, but nothing to commit.

*By submitting this pull request, I confirm that my contribution is made under the terms of the [Wing Cloud Contribution License](https://github.com/winglang/wing/blob/main/CONTRIBUTION_LICENSE.md)*.
@staycoolcall911 staycoolcall911 removed the good first issue Good for newcomers label May 5, 2024
@staycoolcall911
Copy link
Contributor

This issue seems to be blocked by #435 - see the discussion on #5609 for more details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
✨ enhancement New feature or request needs-discussion Further discussion is needed prior to impl 🎨 sdk SDK
Projects
Status: 🤝 Backlog - handoff to owners
Development

No branches or pull requests

7 participants