Today, I came forward to solve one tricky problem, by the way it's very common problems, get an array of unsorted numbers to be sorted incrementally.
It just a few lines of codes:
Public Function SortArrayIncrement(ByVal ArrNum As ArrayList) As ArrayList
For i As Integer = 0 To ArrNum.Count - 1
For j As Integer = i To ArrNum.Count - 1
If ArrNum(i) > ArrNum(j) Then
Dim NewLowest As Integer = ArrNum(j)
ArrNum(j) = ArrNum(i)
ArrNum(i) = NewLowest
End If
Next
Next
Return ArrNum
End Function
From anywhere of your the project file, you can call this function such as
Dim m_arrNumSorted as ArrayLIst = SortArrayIncrement(m_arrNum)
To try out this function, follow this:
Private Sub btnSort_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSort.Click
m_arrNum.Add("9")
m_arrNum.Add("5")
m_arrNum.Add("4")
m_arrNum.Add("7")
m_arrNum.Add("3")
m_arrNum.Add("1")
Dim m_arrNumSorted as ArrayLIst = SortArrayIncrement(m_arrNum)
Dim strNumSorted as String = ""
For k As Integer = 0 To m_arrNumSorted.Count - 1
If k = 0 Then
strNumSorted = m_arrNumSorted(k)
Me.txtArraySort.Text = strNumSorted
Else
strNumSorted = strNumSorted & ", " & m_arrNumSorted(k)
Me.txtArraySort.Text = strNumSorted
End If
Next
End Sub
Once, you click the btnSort, you will see a sorted array of numbers in the textbox called txtArraySort.
Just sharing.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment