visual basic array sorting from list box

时间:2017-04-06 16:54:44

标签: arrays vb.net sorting arraylist

Hello im having trouble with my code ! we are asked to organize a list of names from a text.txt file the make them show up into a lits box (got that part down :) ) . then from the list box we are asked to create an array and sort that array (using our own sorting method) and organize the names using a button in assending order and another button organizing the array in decending order. the results from the orders names should appear in another list box . i have gotten only the last name in the list to show up in the second list box but my code has no errors it just wont order the names properly! Help!!!!!

here is my code :)

Public Class FileSort
Dim sr As IO.StreamReader = IO.File.OpenText("C:\Users\Inspiron 15\documents\visual studio 2010\Projects\assigment4 EL\assigment4 EL\names.txt")

Structure names
Dim c As Integer
Dim fullname As String
End Structure

Dim allNames(99) As names

Private Sub btnName_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnName.Click

 Do While sr.Peek <> -1
    Name = sr.ReadLine
    LstNames.Items.Add(Name & " ")
Loop
sr.Close()

End Sub
Private Sub bubbelsort(ByRef names() As System.String, ByVal c As Integer)
c = 0
names(c) = sr.ReadLine()
c = c * 1
For c = 1 To 99 Step +1 '~~~ Addding (Z to A) to the the Listbox
    lstOrderedNames.Items.Add(Name & "")'
Next

End Sub
Private Sub BtnAssend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAssend.Click

Dim names(99) As String
Dim c As Integer
c = 0
Dim A As Integer
A = 99
names(c) = sr.ToString
c = c + 1
For c = 1 To 99 Step +1 '~~~ Addding (Z to A) to the the Listbox
    lstOrderedNames.Items.Add(Name & "")
Next
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDessend.Click
Dim names(99) As String
Dim c As Integer
c = 0
Dim A As Integer
A = 99
names(c) = sr.ToString
names(A) = sr.ToString
A = A - 1
For A = 99 To 0 Step -1 '~~~ Addding (Z to A) to the the Listbox
    lstOrderedNames.Items.Add(Name & "")
Next
End Sub

enter image description here

2 个答案:

答案 0 :(得分:0)

你可以使用linq

 ListBox1.Items.Add("Battle")
 ListBox1.Items.Add("Cattle")
 ListBox1.Items.Add("apple")

 ListBox2.DataSource = (From l In ListBox1.Items
                        Select l Order By l Ascending).ToList

答案 1 :(得分:0)

因为您的问题是排序算法(如果我理解正确的话)。

首先我们需要一个数组。

Dim arr(ListBox1.Items.Count - 1) As String
For i As Integer = 0 To arr.Length - 1
    arr(i) = CStr(ListBox1.Items(i))
Next

接下来的排序算法。既然你想要使用BubbleSort:

Private Sub StringBubbleSort(arr As String)
    For i As Integer = 0 To arr.Length - 1
        For j As Integer = 0 To arr.Length - 2 - i
            If String.Compare(arr(j), arr(j + 1)) > 0 Then
                Dim temp As String = arr(j)
                arr(j) = arr(i)
                arr(i) = temp
            End If
        Next
    Next
End Sub

然后使用此函数并将数组复制到第二个ListBox。

StringBubbleSort(arr)
ListBox2.Items.AddRange(arr)

String.Compare:https://msdn.microsoft.com/de-de/library/84787k22(v=vs.110).aspx