Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 基于自定义对象中的ArrayList对列表进行排序_Vb.net_Sorting_.net 2.0 - Fatal编程技术网

Vb.net 基于自定义对象中的ArrayList对列表进行排序

Vb.net 基于自定义对象中的ArrayList对列表进行排序,vb.net,sorting,.net-2.0,Vb.net,Sorting,.net 2.0,我使用列表跟踪许多自定义行对象,如下所示: Public Rows As List(Of Row)() 行有两个属性,键(一个字符串)和单元格(一个数组列表) 我需要能够根据单元格数组列表的开发人员定义的索引,对行中的每个行进行排序 例如,基于以下行s Row1.Cells = ("b", "12") Row2.Cells = ("a", "23") 行。排序(0)将导致行2在行列表中位于第一位。实现这一点的最佳方式是什么 谢谢如果您在行对象上实现IComparable,您可以定义一个自定义

我使用列表跟踪许多自定义
对象,如下所示:

Public Rows As List(Of Row)()
有两个属性,
(一个字符串)和
单元格
(一个数组列表)

我需要能够根据
单元格
数组列表的开发人员定义的索引,对
中的每个
进行排序

例如,基于以下
s

Row1.Cells = ("b", "12")
Row2.Cells = ("a", "23")
行。排序(0)
将导致
行2
列表中位于第一位。实现这一点的最佳方式是什么


谢谢

如果您在行对象上实现IComparable,您可以定义一个自定义比较过程,以便能够执行所需的排序


这是一个让您开始学习的方法。

它类似于带有列的DatRow,因此您可以替代(IComparable解决方案)使用DataView的排序功能:

考虑动态创建一个DataTabale,将其列作为单元格。 例如:

Dim rows As New List(Of Row)
Dim row = New Row("Row 1")
Dim cell1 As New Row.Cell("b")
Dim cell2 As New Row.Cell("12")
row.Cells.Add(cell1)
row.Cells.Add(cell2)
rows.Add(row)
row = New Row("Row 2")
cell1 = New Row.Cell("a")
cell2 = New Row.Cell("23")
row.Cells.Add(cell1)
row.Cells.Add(cell2)
rows.Add(row)

Dim tbl As New DataTable()
''#are the cell count in every row equal? Otherwise you can calculate the max-count
Dim cellCount As Int32 = 2 ''#for example
For i As Int32 = 0 To cellCount - 1
    Dim newCol As New DataColumn("Column " & i + 1)
    tbl.Columns.Add(newCol)
Next
For rowIndex As Int32 = 0 To rows.Count - 1
    row = rows(rowIndex)
    Dim newRow As DataRow = tbl.NewRow
    For cellIndex As Int32 = 0 To row.Cells.Count - 1
        Dim cell As Row.Cell = row.Cells(cellIndex)
        newRow(cellIndex) = cell.value
    Next
    tbl.Rows.Add(newRow)
Next
Dim view As New DataView(tbl)
view.Sort = "Column 1"
''#you can use the view as datasource or other purposes

谢谢你,IComparable对我的
对象进行了一些调整。很高兴它有帮助,前几次有点棘手,但对这些自定义排序项目非常有用