Question

I have a collection of SortedDictionary:

SortedDictionary<string, string> _variableValues = new SortedDictionary<string, string>();

The key value pairs look like:

ThkPost_Point15  5
ThkPost_Point1   15
ThkPost_Point11  8
ThkPost_Point2   7
ThkPost_Point20  1
ThkPost_Point22  1

Now i want to sort them alphabetically.

I tried to do this by using

_variableValues.OrderBy(key => key.Key);

But my problem is that the result of that is:

ThkPost_Point1   15
ThkPost_Point11  8
ThkPost_Point15  5
ThkPost_Point2   7
ThkPost_Point20  1
ThkPost_Point22  1

But what im looking for is a solution to achieve a sort result like:

ThkPost_Point1   15
ThkPost_Point2   7
ThkPost_Point11  8
ThkPost_Point15  5
ThkPost_Point20  1
ThkPost_Point22  1

Is there any way to sort data in this way using SortedDictionary?

Edit:

I tried to implement the hints of Babu and metacubed.

So impelented it in the following way

    static void Main(string[] args)
    {
        SortedDictionary<string, string> _varVal = new SortedDictionary<string, string>(StringComparer.OrdinalIgnoreCase);

        _varVal.Add("Variable1", "Value1");
        _varVal.Add("Variable2", "Value2");
        _varVal.Add("Variable3", "Value3");
        _varVal.Add("Variable4", "Value4");
        _varVal.Add("Variable5", "Value5");

        _varVal.Add("Variable10", "Value10");
        _varVal.Add("Variable11", "Value11");
        _varVal.Add("Variable12", "Value12");
        _varVal.Add("Variable13", "Value13");
        _varVal.Add("Variable14", "Value14");

        _varVal.Add("Variable20", "Value20");
        _varVal.Add("Variable22", "Value21");
        _varVal.Add("Variable23", "Value22");
        _varVal.Add("Variable24", "Value23");
        _varVal.Add("Variable25", "Value24");

        foreach (KeyValuePair<string, string> kvp in _varVal)
        {
            Console.WriteLine(kvp.Key,kvp.Value);
            Debug.WriteLine(kvp.Key +" "+ kvp.Value);
        }

        Application.Run();
    }

But the result is still as following:

    Variable1 Value1
    Variable10 Value10
    Variable11 Value11
    Variable12 Value12
    Variable13 Value13
    Variable14 Value14
    Variable2 Value2
    Variable20 Value20
    Variable22 Value21
    Variable23 Value22
    Variable24 Value23
    Variable25 Value24
    Variable3 Value3
    Variable4 Value4
    Variable5 Value5

Did i miss anything?

Was it helpful?

Solution

NOTE: Revising because the earlier answer did not correctly solve the problem.

What you are looking for is called "natural order" sorting. This is a lot more complicated to do correctly than earlier solutions. In short, you have to split the string key into its number and text parts, and then sort each part in order. If the part is number-like, sort it like a number. Otherwise, sort it as a string.

See this blog post Natural Sort Compare with Linq OrderBy for a possible implementation. The original source is here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top