Skip to content

[API Proposal]: Sort() on OrderedDictionary #115127

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

Open
jhudsoncedaron opened this issue Apr 28, 2025 · 4 comments
Open

[API Proposal]: Sort() on OrderedDictionary #115127

jhudsoncedaron opened this issue Apr 28, 2025 · 4 comments
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Collections untriaged New issue has not been triaged by the area owner

Comments

@jhudsoncedaron
Copy link

Background and motivation

We've had an OrderedDictionary<TK, TV> for a long time; and up comes a stock OrderedDictionary in .NET 9. Kind of an ugly breaking change but there's things I can do about that.

I'd like to replace my junky implementation with the stock implementation; however in order to do that I would need a method added to the stock OrderedDictionary: Sort()

API Proposal

namespace System.Collections.Generic;

public class OrderedDictionary<TK, TV> : /* unchanged */
{
    /* Actual method needed */
    public void Sort(int index, int count, IComparer<KeyValuePair<TK, TV>> comparer);

    /* Standard expected helper method */
    public void Sort(IComparer<KeyValuePair<TK, TV>> comparer) => Sort(0, Count, comparer);
}

API Usage

OrderedDictionary<long, ListItem<long, string>> GetList()
{
    var dict = new OrderedDictionary<long, ListItem<long, string>>();
    /* Populate the dictionary; say from an SQL Query */
    
    dict.Sort(0, dict.Length, new IndirectComparer());
    return dict;
}

private class IndirectComparer: IComparer<KeyValuePair<long, ListItem<long, string>> {
    int Compare(KeyValuePair<long, ListItem<long, string> x, KeyValuePair<long, ListItem<long, string> y)
    {
        if (x.Value is null) return y.Value is null ? 0 : -1;
        if (y.Value is null) return 1;
        return x.Value.CompareTo(y.Value); /* Assume this handles localized sort */
    }
}

Alternative Designs

Add the Swap(int, int) method and I can provide the sort. Without the Swap method an external sort will perform horribly and I must keep using my own junky implementation.

Risks

Potential source breaking change if somebody added a Sort() method into a derived class.

@jhudsoncedaron jhudsoncedaron added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Apr 28, 2025
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Apr 28, 2025
Copy link
Contributor

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

@jhudsoncedaron
Copy link
Author

@colejohnson66 : You appear to be thinking of SortedDictionary. OrderedDictionary remembers the order that items were inserted in.

@kronic
Copy link
Contributor

kronic commented Apr 30, 2025

Dublicate

@jhudsoncedaron
Copy link
Author

@kronic : I think I did a slightly better job of writing down the API definition; but the other request is essentially the same and I don't know why every list sort exposes the sort partial range function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Collections untriaged New issue has not been triaged by the area owner
Projects
None yet
Development

No branches or pull requests

2 participants