none
Struggling with Lambdas and Sorting with list.OrderBy RRS feed

  • Question

  • What am I doing wrong?  The list remains unsorted?

              

      List<string> list = new List<string>() { "banana", "apple",  "cat", "dogs", "eel" };
    
                var byStringLength = Comparer<string>.Create((s1, s2) => s1.Length.CompareTo(s2.Length));
                var ordered = list.OrderBy(c => byStringLength);
                foreach (var word in ordered)
                {
                           Console.WriteLine($"'{w}'={w.Length} ");
                     }
    
                result:
                  'banana'=6
                  'apple'=5 
                  'cat'=3 
                  'dogs'=4 
                  'eel'=3

    Thursday, February 15, 2018 5:07 PM

Answers