[AccessD] sorting a list

Stuart McLachlan stuart at lexacorp.com.pg
Mon Apr 21 21:51:56 CDT 2003


On 21 Apr 2003 at 20:51, Susan Harkins wrote:

> Anyone have an easy way to sort a list or combo control's list when
> working with a Value List RowSourceType?
> 
Assuming you are using A2K with it's Split and Join functions:


Option Compare Database

Private Sub btnSortList_Click()

Dim strTempArray() As String
'create the array and
'Put a dummy space in element 0 of array
strTempArray() = Split(" ;" & List0.RowSource, ";")

'sort the array
Call Quicksort(strTempArray)

'Don't forget to strip element 0 again
List0.RowSource = Mid$(Join(strTempArray(), ";"), 3)
End Sub


Sub Quicksort(X$())
    'quicksort sort of string array x$()
    ' VERY old QBasic code
    ' - but I've never needed to update it
    'Note: Sorts elements X$(1) to UBound(X$)
    ' That's why we've padded the above Split with an initial element
  n% = UBound(X$)
  m% = 12
  ReDim stack%(m%, 2)
  s% = 1
  stack%(1, 1) = 1: stack%(1, 2) = n%
spopstack:
    l% = stack%(s%, 1): r% = stack%(s%, 2): s% = s% - 1
sdividelr:
    i% = l%: j% = r%: a$ = X$(Int((l% + r%) / 2))
sscanr:
    If X$(i%) < a$ Then i% = i% + 1: GoTo sscanr
sscanl:
    If X$(j%) > a$ Then j% = j% - 1: GoTo sscanl
    If i% > j% Then GoTo snextpart
    w$ = X$(i%): X$(i%) = X$(j%): X$(j%) = w$
    i% = i% + 1: j% = j% - 1
    If i% > j% Then GoTo snextpart
    GoTo sscanr
snextpart:
    If j% - l% < r% - i% And i% < r% Then s% = s% + 1: stack%(s%, 1) 
= i%: stack%(s%, 2) = r%
    If j% - l% >= r% - i% And l% < j% Then s% = s% + 1: stack%(s%, 1) 
= l%: stack%(s%, 2) = j%
    If j% - l% < r% - i% Then r% = j% Else l% = i%
    If l% >= r% Then GoTo sexitloop
    GoTo sdividelr
sexitloop:
    If s% = 0 Then Exit Sub
    GoTo spopstack
End Sub

-- 
Lexacorp Ltd
http://www.lexacorp.com.pg
Information Technology Consultancy, Software Development,System 
Support.





More information about the AccessD mailing list