gpt4 book ai didi

vb.net - 在 VB 中对列表框项目进行数字排序

转载 作者:行者123 更新时间:2023-12-01 13:07:16 25 4
gpt4 key购买 nike

我需要按数字对 Visual Basic 列表框中的项目进行排序,也就是说,我有一个数字集合,我希望对其进行越来越多的排序。

我尝试简单地使用列表框的 Sorted 属性,但发现它将数字视为字符串,也就是说,它会查看第一个数字,然后是第二个数字,依此类推来确定顺序。例如,这意味着 13 会在 5 之前显示。

我想将所有数字转储到一个数组中,对数组进行排序,然后将它们推回列表框,但老实说,我不知道如何进行排序。我认为数组是无用的,因为列表框已经充当伪数组。

有什么想法吗?

最佳答案

你可以这样使用:

Private Shared Sub SortIntegerListBox(ByVal listBox As ListBox)
Dim TempList As New List(Of Integer)
For Each LI In listBox.Items
TempList.Add(Integer.Parse(LI.ToString()))
Next
TempList.Sort()
listBox.DataSource = TempList
End Sub

绑定(bind)后调用:

    Dim Items As New List(Of Integer)
Items.Add(1)
Items.Add(13)
Items.Add(2)

Me.ListBox1.DataSource = Items
SortIntegerListBox(Me.ListBox1)

关于vb.net - 在 VB 中对列表框项目进行数字排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2157383/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com