Assuming that the Key is always a string
as the input has a comma separated value format; something along these lines should do the trick.
public static SortedList<string, TValue> DoSList<TValue>(string csv) where TValue : IConvertible
{
SortedList<string, TValue> target = new ();
string[] firstSplitArray = csv.Split('|');
if (firstSplitArray.Length > 0)
{
foreach (string t1Instance in firstSplitArray)
{
string[] secondSplitArray = t1Instance.Split(';');
if (secondSplitArray.Length == 2)
{
{
var result= (TValue)Convert.ChangeType(secondSplitArray[1], typeof(TValue));
target.Add(secondSplitArray[0], result);
}
}
}
}
return target;
}
It can be used like this:-
string testStr = "Y;5|X;6|W;7|V;8|A;1|B;2|C;3|D;4";
SortedList<string, int> sortedList = DoSList<int>(testStr);
foreach (var kvp in sortedList)
Console.WriteLine($"{kvp.Key}:{kvp.Value}");