I am new to C# and Blazor. I'm trying to create a column in a table with a filter pop-up similar to MS Excel's filtered columns. I need a distinct list of values and so I used a SortedSet
I called UniqueValues. When they are selected, I am also storing them in a SortedSet
called SelectedValues so that I don't need to convert anything.
public readonly SortedSet<string> UniqueValues = new SortedSet<string>(StringComparer.OrdinalIgnoreCase);
public SortedSet<string> SelectedValues { get; set; } = new (StringComparer.OrdinalIgnoreCase);
The problem is with my data that has both numbers & strings in it, the SortedSet of course doesn't sort it in the expected numerical way since it is of type string
. To get around a similar issue I was having with OrderBy
for an ICollection<T>
I made this method:
public static class StringExtensions
{
public static string NumberPadded(this string text)
{
if (string.IsNullOrWhiteSpace(text)) return string.Empty;
return Regex.Replace(text, "[0-9]+", (Match m) => m.Value.PadLeft(10, '0'));
}
}
I then called this method where I'm sorting my column data, which looks kind of like public ICollection<T> SortData { return source.OrderBy(x => NumberPadded(x)); }
This works, but is there a way to declare a SortedSet as alphanumeric?
I'm trying to keep my UniqueValues and SelectedValues as generic/dynamic as possible so they can be used across multiple data types & I need my data to display sorted and with distinct values only. Would it be better to use a HashSet and the OrderBy function or something? Here is what it looks like currently, as you can see my "Name" column is sorted correctly using the NumberPadded method, but my pop-up isn't
I've looked at Problem with adding sorted number Strings to a SortedSet in Java but I am using C#