Sort List(of Object) by object properties

前端 未结 3 1276
南方客
南方客 2021-01-26 02:13

I\'m trying to achieve something where the answer is already given for. But it\'s in c# and I don\'t have any knowledge what-so-ever over c# so I\'m lo

3条回答
  •  轮回少年
    2021-01-26 02:25

    This C# code from that other thread:

    List SortedList = objListOrder.OrderBy(o=>o.OrderDate).ToList();
    

    equates to this VB code:

    List(Of Order) SortedList = objListOrder.OrderBy(Function(o) o.OrderDate).ToList()
    

    As you can see, very little changes. You just have to know the syntax for generics and lambda expressions.

    You should be aware, though, that this code does NOT sort your list. It sorts the items in the list and then adds them to a new list in that order. This is perfectly fine for many applications but if you're using that same list elsewhere then you won't see the new order there. While there are a few variations, one way to actually sort the list in place is like this:

    objListOrder.Sort(Function(o1, o2) o1.OrderDate.CompareTo(o2.OrderDate))
    

提交回复
热议问题