Lambda OrderBy method

27,176

OrderByDescending does not reorder the list in-place. It returns an enumerator that you can use to get a new ordered list. You need to use .ToList() to replace the original list:

 nodeList = nodeList.OrderByDescending(Function(x) x.Count).ToList()
Share:
27,176
Princess
Author by

Princess

Nothing else to see here!

Updated on July 12, 2022

Comments

  • Princess
    Princess almost 2 years

    I have a graphing class to detect circular dependencies in some business logic I am writing. My graphing class builds nodes that knows the relationship to other nodes.

    I have nodeList as List(of Objects) each having a List(of String)

    I was thinking that the below line of code would yield the correct sorting. I thought wrong.

    nodeList.OrderByDescending(Function(x) x.Count)
    

    I want to reorder my nodeList in descending order by the List(of String).Count.

    my List(of Object)
    (0) | Count = 3
    (1) | Count = 5
    (2) | Count = 2
    
    My desired output List(of Object)
    (0) | Count = 5
    (1) | Count = 3
    (2) | Count = 2
    
  • Dylan Meador
    Dylan Meador over 11 years
    This assumes that each node has a Count property that returns the Count of the contained string list. Something like x.MyStringList.Count might be worth mentioning.