Skip to content

[API Proposal]: Provide public implementation to sort IList<T> instances in place #101883

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

Closed
vvdev opened this issue May 5, 2024 · 2 comments
Closed
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Collections

Comments

@vvdev
Copy link

vvdev commented May 5, 2024

Background and motivation

.NET has a great implementations and public APIs for in place sorting of List<>, Array and Span.
Even more code used by these public APIs is internal/private.

Would be great to reuse the same algorithms (and code when possible) for in place sorting of any IList implementation.

In-place sorting is needed to avoid excessive memory allocations.

Of course, resulting performance will not be as optimal as direct Span/Array sorting and will be highly dependent on concrete IList<> implementation, but at least it [in most of the cases] will be better than most of [quick] implementations made by developers in their codebases.

API Proposal

namespace System.Collections.Generic;

public static class IListSortExtensions<T>
{
    public static void Sort<T>(this IList<T> list) => list.Sort(Comparer<T>.Default);
    public static void Sort<T, TComparer>(this IList<T> list, TComparer comparer) where TComparer : IComparer<T>;

   // also these methods to sort struct lists without boxing
    public static void Sort<T, TList>(this ref TList list) => list.Sort(Comparer<T>.Default) where TList : IList<T>;
    public static void Sort<T, TList, TComparer>(this ref TList list, TComparer comparer) where TList : IList<T> where TComparer : IComparer<T>;
}

API Usage

private void SomeMethod(IList<int> list)
{
    list.Sort();
   // do stuff
}

public struct StructBuffer<T> : IList<T>
{
//
}

private void SomeMethod(StructBuffer<int> list)
{
    list.Sort<int, StructBuffer<int>>();
    // do stuff
}

### Alternative Designs

_No response_

### Risks

_No response_
@vvdev vvdev added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label May 5, 2024
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label May 5, 2024
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-collections
See info in area-owners.md if you want to be subscribed.

@stephentoub
Copy link
Member

Duplicate of #76375

@stephentoub stephentoub marked this as a duplicate of #76375 May 5, 2024
@stephentoub stephentoub closed this as not planned Won't fix, can't repro, duplicate, stale May 5, 2024
@dotnet-policy-service dotnet-policy-service bot removed the untriaged New issue has not been triaged by the area owner label May 5, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Jun 5, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Collections
Projects
None yet
Development

No branches or pull requests

2 participants