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

Custom order of parameters when using complex types #2301

Closed
mrahhal opened this issue May 3, 2023 · 2 comments · Fixed by #2304
Closed

Custom order of parameters when using complex types #2301

mrahhal opened this issue May 3, 2023 · 2 comments · Fixed by #2304
Assignees

Comments

@mrahhal
Copy link
Contributor

mrahhal commented May 3, 2023

I originally had the following int parameter:

[Params(1_000, 10_000, 100_000, 1_000_000, 10_000_000)]
public int N;

This sorted properly using the natural order of the int.

I then wanted to customize the value names (1K, 1M, etc instead), so I had to wrap the int in a complex type to override ToString() (just a simple class with an int and a string, and then using a ParamsSource of a list).

I now had correct names, but since the parameter is now a complex type, it appears that the display name is used to do an alphabetical sort instead. This causes the order to be something like "100K, 10K, 10M, 1K, etc" , which I definitely don't want.

I can see the current implementation of this here:

return string.CompareOrdinal(x.DisplayInfo, y.DisplayInfo);

As far as I can tell, there doesn't seem to be a way to get both a customized display but also ordering according to the integer (or some other custom ordering), without doing something extreme like having to provide an implementation of IOrderer for example (which doesn't seem at all feasible to me).

I see a mention of a proposed ParamsOrderPolicy here, but I think that doesn't provide enough flexibility.

I'm proposing a change in ParameterComparer, where complex params are tested if they're IComparable, and then simply use that interface to provide the order behavior.

I'm willing to work on this, as I feel it's a must have feature.

@AndreyAkinshin
Copy link
Member

where complex params are tested if they're IComparable, and then simply use that interface to provide the order behavior.

It's a great idea, thanks for the suggestion!

I'm willing to work on this, as I feel it's a must have feature.

PR is welcome!

@mrahhal
Copy link
Contributor Author

mrahhal commented May 9, 2023

Great! Will work on a PR soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants