如何在 VB.Net 中对 System.Collections.Generic.List 进行排序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/438715/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 10:58:27  来源:igfitidea点击:

How sort a System.Collections.Generic.List in VB.Net?

.netvb.netsorting

提问by Nakul Chaudhary

I using a genric list(m_equipmentList ) which is collection of objects (Schedule_Payitem).
How can sort list according to a proerty of child object ?

我使用了一个通用列表(m_equipmentList),它是对象的集合(Schedule_Payitem)。
如何根据子对象的属性对列表进行排序?

Dim m_equipmentList As New List(Of Schedule_Payitem)

Need to sort m_equipmentList on basis of resourceid property of Schedule_Payitem.

需要根据 Schedule_Payitem 的 resourceid 属性对 m_equipmentList 进行排序。

回答by Jon Skeet

Are you using VB9? If so, I'd use a lambda expressionto create a Comparer(Of Schedule_PayItem). Otherwise, write a short class to implement IComparer(Of Schedule_PayItem). pass whichever one you've got into List.Sort.

你用的是VB9吗?如果是这样,我会使用lambda 表达式来创建一个Comparer(Of Schedule_PayItem). 否则,编写一个简短的类来实现IComparer(Of Schedule_PayItem)。传递任何一个你进入 List.Sort。

An example for the lambda expression (untested):

lambda 表达式的示例(未经测试):

m_equipmentList.Sort(Function(p1, p2) p1.ResourceID.CompareTo(p2.ResourceID))

And for the IComparer(Of Schedule_PayItem):

而对于IComparer(Of Schedule_PayItem)

Public Class PayItemResourceComparer
    Implements IComparer(Of Schedule_PayItem)
    Public Function Compare(ByVal p1 As Schedule_PayItem, _
                            ByVal p2 As Schedule_PayItem) As Integer
        Return p1.ResourceID.CompareTo(p2.ResourceID)
    End Function
End Class

...

m_equipmentList.Sort(New PayItemResourceComparer)

回答by Pablo Retyk

I don't know vb.net so I did it in C#

我不知道 vb.net 所以我用 C# 做的

m_equipmentList.Sort(
   (payItem1,payItem2)=>payItem1.ResourceID.CompareTo(payItem2.ResourceID));

and using the reflector translated it to vb.net hope it helps

并使用反射器将其翻译为 vb.net 希望它有所帮助

m_equipmentList.Sort(
Function (ByVal payItem1 As Schedule_Payitem, ByVal payItem2 As Schedule_Payitem) 
    Return payItem1.ResourceID.CompareTo(payItem2.ResourceID)
End Function)

or you can inherit Schedule_Payitem from IComparable and implement CompareTo and then just call m_equipmentList.Sort()

或者你可以从 IComparable 继承 Schedule_Payitem 并实现 CompareTo 然后调用 m_equipmentList.Sort()

回答by rgb

You can accomplish sorting the list in descending order by changing this-

您可以通过更改此来完成按降序对列表进行排序 -

m_equipmentList.Sort(
Function (ByVal payItem1 As Schedule_Payitem, ByVal payItem2 As Schedule_Payitem) 
    Return payItem1.ResourceID.CompareTo(payItem2.ResourceID)
End Function)

to this

对此

m_equipmentList.Sort(
Function (ByVal payItem1 As Schedule_Payitem, ByVal payItem2 As Schedule_Payitem) 
    Return payItem2.ResourceID.CompareTo(payItem1.ResourceID)
End Function)

回答by cmsjr

Try this

尝试这个

Dim m_equipmentList As New List(Of Schedule_Payitem)


m_equipmentList.Sort(delegate(Schedule_Payitem p1, Schedule_Payitem p2)
              {
                  return p1.resourceid .CompareTo(p2.resourceid );
              });