Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sort multiple-field data with bubble sort

Status
Not open for further replies.

cluM09

Technical User
May 15, 2004
127
US
Hello,

I have used the bubble sort to a one dimensional array data, and it appeared to work fine. The sort function is listed below:

Function SortItem(arrSort)
Dim k, j, temp
For k = UBound(arrSort) - 1 To 0 Step -1
For j= 0 To k
If Lcase(arrSort(j))>LCase(arrSort(j+1)) Then
temp=arrSort(j+1)
arrSort(j+1)=arrSort(j)
arrSort(j)=temp
End If
Next
Next
SortItem = arrSort
End Function

I would like to know if I can use the bubble sort to sort data with multiple fields (or multi-dimensional arrays). Any help would be greatly appreciated!

The data I need to have sorted are listed below:

Field1 Field2 Field3
Ashley AAbraham QPO
Abraham ABalla ABC
Agarwal Abhishek KLP
Aghapou Ali 0KJ
Bhawsar Andrew POFS
Al-Rayess Annette MJKL
Aihie AAlRayess FDS
Amigleo Arsenio HYG
Ballard Anna GNV
Bhawsar Abhijit WJKG

Thanks!

CluM09
 
Test a concatenation of your sort fields.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
PHV,

Thanks for the response! How do you test the concatenation of the sort fields?

I got the following multidimensional sort function from the net at but I don't know how to use it. Maybe I need to create a multidimensional array data from the table before I can use it. If so, how can I create a multidimensional array from the data listed above? I am new to VBscript, and I have never create a multidimensional array from a table.
 
chuck it into a recordset and use standard SQL stuff on it
 
mrmovie,

Thanks for the response! I can sort the data with ADO, but I just want to check and see if it is possible to sort with the following function which I got the link above. It claims that it can sort a multidimensional array.

Function ArraySort(values(),intSortCol,strDirection)
Dim i
Dim j
Dim value
Dim value_j
Dim min
Dim max
Dim temp
Dim datatype
Dim intComp
Dim intA
Dim intCheckIndex

min = LBound(values,2)
max = UBound(values,2)

' check to see what direction you want to sort.
If LCase(strDirection) = "d" Then
intComp = -1
Else
intComp = 1
end if

If intSortCol < 0 or intSortCol > UBound(values,1) Then
arraysort = values
Exit Function
End If
' find the first item which has valid data in it to sort
intCheckIndex = min
While Len(Trim(values(intSortCol,intCheckIndex))) = 0 And intCheckIndex < UBound(values,2)
intCheckIndex = intCheckIndex + 1
Wend
If isDate(Trim(values(intSortCol,intCheckIndex))) Then
datatype = 1
Else
If isNumeric(Trim(values(intSortCol,intCheckIndex))) Then
datatype = 2
Else
datatype = 0
End If
End If
For i = min To max - 1
value = values(intSortCol,i)
value_j = i
For j = i + 1 To max
Select Case datatype
Case 0
' See if values(j) is smaller. works with strings now.
If strComp(values(intSortCol,j),value,vbTextCompare) = intComp Then
' Save the new smallest value.
value = values(intSortCol,j)
value_j = j
End If
Case 1
If intComp = -1 Then
If DateDiff("s",values(intSortCol,j),value) > 0 Then
' Save the new smallest value.
value = values(intSortCol,j)
value_j = j
End If
Else
If DateDiff("s",values(intSortCol,j),value) < 0 Then
' Save the new smallest value.
value = values(intSortCol,j)
value_j = j
End If
End If
Case 2
If intComp = -1 Then
If Cdbl(values(intSortCol,j)) < Cdbl(value) Then
' Save the new smallest value.
value = values(intSortCol,j)
value_j = j
End If
Else
If Cdbl(values(intSortCol,j)) > Cdbl(value) Then
' Save the new smallest value.
value = values(intSortCol,j)
value_j = j
End If
End If
End Select
Next 'j
If value_j <> i Then
' Swap items i and value_j.
For intA = 0 To UBound(values,1)
temp = values(intA,value_j)
values(intA,value_j) = values(intA,i)
values(intA,i) = temp
Next 'intA
End If
Next 'i
ArraySort = values
End Function
 
Bubble sort... I haven't used that in YEARS.... as I recall, it was the slowest of the sort routines.....



Just my 2¢

"In order to start solving a problem, one must first identify its owner." --Me
--Greg
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top