在 VS2012 中使用 Visual Basic 对数组进行降序排序
Sorting an array in descending order using visual basic in VS2012
我正在制作一个程序,通过单击按钮将数字随机生成到 ListBox 中的 100 行,每行 15 个。我需要将它从大到小排序,从左到右跨行。我有一个冒泡排序,但它是从小到大排序的,并且只在第一列下方。
这就是我生成数字的方式:
Private Sub btnGen_Click(sender As Object, e As EventArgs) Handles btnGen.Click
'Number Generator
Dim rn As New Random()
Dim array(14) As Integer
Dim temp As Integer
Dim st As String
For y As Integer = 1 To 100
For x As Integer = 1 To 15
array(x - 1) = rn.Next(100, 1000)
Next
txtList.Items.Add(ats(array))
Next
st = st & vbNewLine
Call sort()
Using fs As New FileStream(My.Settings.DAT_PATH, FileMode.Append, FileAccess.Write)
Using sw As New StreamWriter(fs)
sw.WriteLine()
End Using
End Using
End Sub
Function ats(ar As Integer()) As String
'FUNCTION for array to string seperated by comma
Dim sb As New System.Text.StringBuilder
For x As Integer = 0 To UBound(ar)
If x = UBound(ar) Then
sb.Append(ar(x).ToString)
Else
sb.Append(ar(x).ToString & ", ")
End If
Next
Return sb.ToString
End Function
我是这样排序的:
Sub sort()
'bubble sort from biggest to smallest
txtList.Sorted = True
Dim array(14) As Integer
Dim temp As Integer
For ipass = 1 To UBound(array)
For i = 0 To UBound(array) - 1
If array(i) > array(i + 1) Then
temp = array(i)
array(i) = array(i + 1)
array(i + 1) = temp
array.Reverse()
End If
Next i
Next ipass
End Sub
最后,这是我当前结果的示例:
107、512、139、233、582、460、698、231、395、724、717、284、699、419、825
119、214、513、382、538、161、431、603、573、354、757、307、204、906、200
124、493、153、507、675、878、698、911、625、171、915、174、270、629、770
126、585、480、317、731、193、385、143、152、374、246、124、205、347、936
139、497、422、381、127、968、236、637、406、758、594、944、929、733、428
如有任何帮助,我们将不胜感激
我按照你的"way"做事。你应该知道有更好的方法可以实现你想要的。
Private Sub btnGen_Click(sender As Object, e As EventArgs) Handles btnGen.Click
'Number Generator
Dim rn As New Random()
Dim array(14) As Integer
Dim temp As Integer
Dim st As String
For y As Integer = 1 To 100
For x As Integer = 1 To 15
array(x - 1) = rn.Next(100, 1000)
Next
'txtList.Items.Add(ats(array))
Next
Dim sortedArray = sort(array)
Using fs As New FileStream(My.Settings.DAT_PATH, FileMode.Append, FileAccess.Write)
Using sw As New StreamWriter(fs)
For Each item In sortedArray
sw.WriteLine(item)
Next
End Using
End Using
End Sub
Function sort(array() As Integer) As IEnumerable(Of Integer)
'bubble sort from biggest to smallest
txtList.Sorted = True
Dim temp As Integer
For ipass = 1 To UBound(array)
For i = 0 To UBound(array) - 1
If array(i) > array(i + 1) Then
temp = array(i)
array(i) = array(i + 1)
array(i + 1) = temp
End If
Next i
Next ipass
Dim sortedArray = array.Reverse()
Return sortedArray
End Function
这是使用专用 class.
的替代解决方案
Input()
方法接受一个布尔开关,允许按升序或降序对 List(Of Integer)
进行排序。
Output()
方法将 return 一个可以传递给 ListBox
的 String()
数组,使用它的 .AddRange()
方法。
Delimiter
属性 可用于指定必须如何分隔字符串组件(在本例中为整数值)。
Private Class SortedLists
Private OutputList As List(Of String)
Public Sub New()
OutputList = New List(Of String)
Delimiter = ", "
End Sub
Public Property Delimiter As String
Public Sub Input(Values As List(Of Integer), Ascending As Boolean)
If Ascending Then
Values.Sort()
Else
Dim IValues As IOrderedEnumerable(Of Integer) = Values.OrderByDescending(Function(i) i)
Values = IValues.ToList()
End If
OutputList.Add(String.Join("", Values.
Select(Function(val, i) (val.ToString &
If(i < Values.Count - 1, Delimiter, "")))))
End Sub
Public Function Output() As String()
Return OutputList.ToArray()
End Function
End Class
重构过程创建随机整数列表,将字符串结果添加到 ListBox
控件,将字符串保存到文件。
整个过程的耗时,用StopWatch
计算,是7~9毫秒。
使用File.WriteAllLines()
经过的时间是10~14毫秒。
Dim rn As New Random()
Dim MySortedLists As New SortedLists
Dim MyIntegerList As New List(Of Integer)
For y As Integer = 1 To 100
For x As Integer = 1 To 15
MyIntegerList.Add(rn.Next(100, 1000))
Next
MySortedLists.Input(MyIntegerList, False)
MyIntegerList.Clear()
Next
txtList.Items.AddRange(MySortedLists.Output())
'File.WriteAllLines is a little slower, but it's easier to read
File.WriteAllLines(My.Settings.DAT_PATH, MySortedLists.Output)
'Using fs As New FileStream(My.Settings.DAT_PATH, FileMode.Append, FileAccess.Write)
' Using sw As New StreamWriter(fs)
' For Each line As String In MySortedLists.Output
' sw.WriteLine(line)
' Next
' End Using
'End Using
我正在制作一个程序,通过单击按钮将数字随机生成到 ListBox 中的 100 行,每行 15 个。我需要将它从大到小排序,从左到右跨行。我有一个冒泡排序,但它是从小到大排序的,并且只在第一列下方。
这就是我生成数字的方式:
Private Sub btnGen_Click(sender As Object, e As EventArgs) Handles btnGen.Click
'Number Generator
Dim rn As New Random()
Dim array(14) As Integer
Dim temp As Integer
Dim st As String
For y As Integer = 1 To 100
For x As Integer = 1 To 15
array(x - 1) = rn.Next(100, 1000)
Next
txtList.Items.Add(ats(array))
Next
st = st & vbNewLine
Call sort()
Using fs As New FileStream(My.Settings.DAT_PATH, FileMode.Append, FileAccess.Write)
Using sw As New StreamWriter(fs)
sw.WriteLine()
End Using
End Using
End Sub
Function ats(ar As Integer()) As String
'FUNCTION for array to string seperated by comma
Dim sb As New System.Text.StringBuilder
For x As Integer = 0 To UBound(ar)
If x = UBound(ar) Then
sb.Append(ar(x).ToString)
Else
sb.Append(ar(x).ToString & ", ")
End If
Next
Return sb.ToString
End Function
我是这样排序的:
Sub sort()
'bubble sort from biggest to smallest
txtList.Sorted = True
Dim array(14) As Integer
Dim temp As Integer
For ipass = 1 To UBound(array)
For i = 0 To UBound(array) - 1
If array(i) > array(i + 1) Then
temp = array(i)
array(i) = array(i + 1)
array(i + 1) = temp
array.Reverse()
End If
Next i
Next ipass
End Sub
最后,这是我当前结果的示例:
107、512、139、233、582、460、698、231、395、724、717、284、699、419、825
119、214、513、382、538、161、431、603、573、354、757、307、204、906、200
124、493、153、507、675、878、698、911、625、171、915、174、270、629、770
126、585、480、317、731、193、385、143、152、374、246、124、205、347、936
139、497、422、381、127、968、236、637、406、758、594、944、929、733、428
如有任何帮助,我们将不胜感激
我按照你的"way"做事。你应该知道有更好的方法可以实现你想要的。
Private Sub btnGen_Click(sender As Object, e As EventArgs) Handles btnGen.Click
'Number Generator
Dim rn As New Random()
Dim array(14) As Integer
Dim temp As Integer
Dim st As String
For y As Integer = 1 To 100
For x As Integer = 1 To 15
array(x - 1) = rn.Next(100, 1000)
Next
'txtList.Items.Add(ats(array))
Next
Dim sortedArray = sort(array)
Using fs As New FileStream(My.Settings.DAT_PATH, FileMode.Append, FileAccess.Write)
Using sw As New StreamWriter(fs)
For Each item In sortedArray
sw.WriteLine(item)
Next
End Using
End Using
End Sub
Function sort(array() As Integer) As IEnumerable(Of Integer)
'bubble sort from biggest to smallest
txtList.Sorted = True
Dim temp As Integer
For ipass = 1 To UBound(array)
For i = 0 To UBound(array) - 1
If array(i) > array(i + 1) Then
temp = array(i)
array(i) = array(i + 1)
array(i + 1) = temp
End If
Next i
Next ipass
Dim sortedArray = array.Reverse()
Return sortedArray
End Function
这是使用专用 class.
Input()
方法接受一个布尔开关,允许按升序或降序对 List(Of Integer)
进行排序。
Output()
方法将 return 一个可以传递给 ListBox
的 String()
数组,使用它的 .AddRange()
方法。
Delimiter
属性 可用于指定必须如何分隔字符串组件(在本例中为整数值)。
Private Class SortedLists
Private OutputList As List(Of String)
Public Sub New()
OutputList = New List(Of String)
Delimiter = ", "
End Sub
Public Property Delimiter As String
Public Sub Input(Values As List(Of Integer), Ascending As Boolean)
If Ascending Then
Values.Sort()
Else
Dim IValues As IOrderedEnumerable(Of Integer) = Values.OrderByDescending(Function(i) i)
Values = IValues.ToList()
End If
OutputList.Add(String.Join("", Values.
Select(Function(val, i) (val.ToString &
If(i < Values.Count - 1, Delimiter, "")))))
End Sub
Public Function Output() As String()
Return OutputList.ToArray()
End Function
End Class
重构过程创建随机整数列表,将字符串结果添加到 ListBox
控件,将字符串保存到文件。
整个过程的耗时,用StopWatch
计算,是7~9毫秒。
使用File.WriteAllLines()
经过的时间是10~14毫秒。
Dim rn As New Random()
Dim MySortedLists As New SortedLists
Dim MyIntegerList As New List(Of Integer)
For y As Integer = 1 To 100
For x As Integer = 1 To 15
MyIntegerList.Add(rn.Next(100, 1000))
Next
MySortedLists.Input(MyIntegerList, False)
MyIntegerList.Clear()
Next
txtList.Items.AddRange(MySortedLists.Output())
'File.WriteAllLines is a little slower, but it's easier to read
File.WriteAllLines(My.Settings.DAT_PATH, MySortedLists.Output)
'Using fs As New FileStream(My.Settings.DAT_PATH, FileMode.Append, FileAccess.Write)
' Using sw As New StreamWriter(fs)
' For Each line As String In MySortedLists.Output
' sw.WriteLine(line)
' Next
' End Using
'End Using