cerr Posted January 29, 2003 Posted January 29, 2003 (edited) Hi guys-- I'm new to VB .net so please bare with me. I am trying to read in a range of values from an excel worksheet and sort them, eventually writting to a seperate file. For now, i have built a multideminsional array of strings w/ the values from excel. I am having some difficulties in developing the algorithm to sort them only on the first column. would appreciate it if someone could please provide some pointers or post some code. Thxs Edited January 29, 2003 by cerr Quote
cerr Posted January 29, 2003 Author Posted January 29, 2003 code OK, so i had *alot* more time to work on this today than what i had thought. here is my initial pass, any comments / suggestions are welcome as i'm pretty new to VB. Module Sort Public Const numDimensions As Integer = 5 Public qstArray(5, numDimensions) As String Sub BubbleSort(ByRef strArray(,) As String, ByVal numDimensions As Integer) 'This function will sort our array. It takes the 2 dim. array that was extracted 'from the excel file and the number of columns. 'Yes, this is a very slow algorithm, but this is only a prototype. This function 'can be optimized in the next release Dim intCounter, EventLoop, i As Integer Dim Temp(1, numDimensions) As String For EventLoop = 1 To UBound(strArray) For intCounter = 1 To UBound(strArray) - 1 If strArray(intCounter, 1) > strArray(intCounter + 1, 1) Then 'swap our values. seperate loops to handle the 2 dimensional array For i = 1 To numDimensions Temp(1, i) = strArray(intCounter, i) Next i For i = 1 To numDimensions strArray(intCounter, i) = strArray(intCounter + 1, i) Next i For i = 1 To numDimensions strArray(intCounter + 1, i) = Temp(1, i) Next i End If Next intCounter Next EventLoop End Sub End Module Quote
TechnoTone Posted January 30, 2003 Posted January 30, 2003 I have a few observations: You have a constant named "numDimensions" and you also have a parameter with the same name. This could get confusing. Arrays are usually zero based but your loops start at one. You could get the lower index from the array itself instead of hard-coding it. You are using UBound which is part of the Microsoft.VisualBasic namespace. I'm unsure of whether there is a .NET equivalent as I am relatively new to .NET but generally speaking you should avoid this namespace unless you have no choice. Quote TT (*_*) There are 10 types of people in this world; those that understand binary and those that don't.
TechnoTone Posted January 30, 2003 Posted January 30, 2003 Arrays are usually zero based but your loops start at one. You could get the lower index from the array itself instead of hard-coding it. On further investigation, according to the MSDN documentation "The lower bound for array subscripts is always 0". Quote TT (*_*) There are 10 types of people in this world; those that understand binary and those that don't.
TechnoTone Posted January 30, 2003 Posted January 30, 2003 Here's one that I've just written. It can work with 1 or 2 dimension arrays and doesn't require additional information about the size of the dimensions as it works them out. I have also optimised the routine. Sub BubbleSort(ByRef stringArray As Array) 'This function will sort an array (1 or 2 dimension only) based on the first dimension. Dim nDimensions As Short Dim nPasses As Short Dim nIndex As Short Dim bSorted As Boolean Dim nCount As Short Dim tempString As String nPasses = 0 nDimensions = stringArray.Rank Do nIndex = 0 bSorted = True While nIndex < stringArray.GetUpperBound(0) - nPasses If nDimensions > 1 Then If stringArray(nIndex, 0) > stringArray(nIndex + 1, 0) Then bSorted = False For nCount = 0 To stringArray.GetUpperBound(1) tempString = stringArray(nIndex, nCount) stringArray(nIndex, nCount) = stringArray(nIndex + 1, nCount) stringArray(nIndex + 1, nCount) = tempString Next End If Else If stringArray(nIndex) > stringArray(nIndex + 1) Then bSorted = False tempString = stringArray(nIndex) stringArray(nIndex) = stringArray(nIndex + 1) stringArray(nIndex + 1) = tempString End If End If nIndex += 1 End While nPasses += 1 Loop Until bSorted Or (nPasses >= stringArray.GetUpperBound(0)) End Sub Sub TestBubbleSort() Dim myArray(4, 4) As String Dim X As Short, Y As Short For X = 0 To 4 For Y = 0 To 4 myArray(X, Y) = Microsoft.VisualBasic.ChrW(69 - X).ToString & (Y + 1).ToString Next Next BubbleSort(myArray) Dim myArray2(9) As String For X = 0 To 9 myArray2(X) = Microsoft.VisualBasic.ChrW(74 - X).ToString Next BubbleSort(myArray2) End Sub Quote TT (*_*) There are 10 types of people in this world; those that understand binary and those that don't.
cerr Posted January 31, 2003 Author Posted January 31, 2003 Hey Techno- Thanks for the pointers! Quote
boanerges Posted March 30, 2003 Posted March 30, 2003 hey folks, im in the middle of writing a prgm to update inventory transactions, im stumped as to what comes next.... i built and initiated the table, i have built the save field in Working storage, and the sort rtn, and initiaon rtn's... what do i include in the sort rtne? does anybody have knowledge of how this needs to be s/u?assignii.txt Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.