Page 1 of 2 12 LastLast
Results 1 to 15 of 21

Thread: Array Sort

  1. #1
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Array Sort

    How does one sort the contents of a Word 97 combobox once the it's populated?
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  2. #2
    Uranium Lounger
    Join Date
    Jan 2001
    Location
    South Carolina, USA
    Posts
    7,295
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Array Sort

    I would copy the items to an array, sort the array, and repopulate the combo box.
    Legare Coleman

  3. #3
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Array Sort

    That's what I thought. Thanks for confirming.
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  4. #4
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Array Sort

    <pre> Public Function SortListBox(lb As ListBox, intCol As Integer)
    If lb.ListCount > 0 Then
    Dim strar() As String
    ReDim strar(lb.ListCount - 1, lb.ColumnCount - 1)
    Dim lngRow As Long
    Dim intColumn As Integer
    For lngRow = 0 To lb.ListCount - 1
    For intColumn = 0 To lb.ColumnCount - 1
    strar(lngRow, intColumn) = lb.List(lngRow, intColumn)
    Next intColumn
    Next lngRow
    Call QCSort(strar, 0, UBound(strar, 1), False, intCol)
    For lngRow = 0 To lb.ListCount - 1
    For intColumn = 0 To lb.ColumnCount - 1
    lb.List(lngRow, intColumn) = strar(lngRow, intColumn)
    Next intColumn
    Next lngRow
    Else
    End If
    End Function
    </pre>






    <pre>Public Function QCSort(strList() As String, ByVal lLbound As Long,_
    ByVal lUbound As Long, ByVal boolCaseSensitive As Boolean, _
    intColumn As Integer)
    ' March 3rd Chris Greaves Implemented a Column key (hence qCsort)
    Dim strTemp As String
    Dim strBuffer() As String
    Dim lngCurLow As Long
    Dim lngCurHigh As Long
    Dim lngCurMidpoint As Long
    lngCurLow = lLbound ' Start current low and high at actual low/high
    lngCurHigh = lUbound
    If lUbound <= lLbound Then Exit Function ' Error!
    lngCurMidpoint = (lLbound + lUbound) 2 ' Find the approx midpoint of the array
    ' Pick as a starting point (we are making
    ' an assumption that the data *might* be in semi-sorted order already!
    strTemp = MyUCase(strList(lngCurMidpoint, intColumn), boolCaseSensitive)
    Do While (lngCurLow <= lngCurHigh)
    Do While MyUCase(strList(lngCurLow, intColumn), boolCaseSensitive) < strTemp
    lngCurLow = lngCurLow + 1
    If lngCurLow = lUbound Then Exit Do
    Loop
    Do While strTemp < MyUCase(strList(lngCurHigh, intColumn), boolCaseSensitive)
    lngCurHigh = lngCurHigh - 1
    If lngCurHigh = lLbound Then Exit Do
    Loop
    If (lngCurLow <= lngCurHigh) Then ' if low is <= high then swap
    Call SwapRows(strList, lngCurLow, lngCurHigh)
    lngCurLow = lngCurLow + 1 ' CurLow++
    lngCurHigh = lngCurHigh - 1 ' CurLow--
    End If
    Loop
    If lLbound < lngCurHigh Then ' Recurse if necessary
    QCSort strList(), lLbound, lngCurHigh, boolCaseSensitive, intColumn
    End If
    If lngCurLow < lUbound Then ' Recurse if necessary
    QCSort strList(), lngCurLow, lUbound, boolCaseSensitive, intColumn
    End If
    'Sub TESTQCSort()
    'Dim strar(6, 3) As String
    'strar(0, 0) = "january"
    'strar(0, 1) = "february"
    'strar(0, 2) = "march"
    'strar(0, 3) = "april"
    'strar(1, 0) = "may"
    'strar(1, 1) = "june"
    'strar(1, 2) = "july"
    'strar(1, 3) = "august"
    'strar(2, 0) = "september"
    'strar(2, 1) = "october"
    'strar(2, 2) = "november"
    'strar(2, 3) = "december"
    'strar(3, 0) = "Monday"
    'strar(3, 1) = "Tuesday"
    'strar(3, 2) = "Wednesday"
    'strar(3, 3) = "Thursday"
    'strar(4, 0) = "Friday"
    'strar(4, 1) = "Saturday"
    'strar(4, 2) = "Sunday"
    'strar(4, 3) = "january"
    'strar(5, 0) = "february"
    'strar(5, 1) = "march"
    'strar(5, 2) = "april"
    'strar(5, 3) = "may"
    'strar(6, 0) = "june"
    'strar(6, 1) = "july"
    'strar(6, 2) = "august"
    'strar(6, 3) = "september"
    'Selection.TypeParagraph
    'Debug.Print " "
    'Call DumpArray(strar)
    'Call QCSort(strar, LBound(strar, 1), UBound(strar, 1), False, 2)
    'Selection.TypeParagraph
    'Debug.Print " "
    'Call DumpArray(strar)
    'End Sub
    End Function
    </pre>


  5. #5
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Array Sort

    Cool dude, but where's the SwapRows function? Thanks though for that time saver. I tell you what. I'll play around with your code and replace your SwapRows with a swap routine that uses the Windows API CopyMemory function:

    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, pSrc As Any, ByVal ByteLen As Long)

    ...if I have time.
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  6. #6
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Array Sort

    <pre> Public Function SwapRows(strList() As String, lngCurLow As Long, lngCurHigh As Long)
    Dim lngCols As Long
    lngCols = UBound(strList, 2)
    Dim strBuffer As String
    Dim lngC As Long
    For lngC = 0 To lngCols
    strBuffer = strList(lngCurLow, lngC)
    strList(lngCurLow, lngC) = strList(lngCurHigh, lngC)
    strList(lngCurHigh, lngC) = strBuffer
    Next lngC
    End Function
    </pre>


  7. #7
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Array Sort

    That's better. Thanks.
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  8. #8
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Array Sort

    You're welcome. I'm not such a bad sort after all, eh?



    You mentioned a (presumably) faster way to sort arrays, In-memory? I'd be interested in a simple example that I could then clone and pass off as my own (grin!). I do a lot of sorting of arrays, listboxes and internal arrays (as, for example, when I am dropping duplicate paragraphs of a document)

    Perhaps we could be nice to the lounge and collaborate on a routine by private email and then publish it? Is that allowed?

  9. #9
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Array Sort

    BTW, your pic is busted.

    Sure. We can to that. You want to get a head start? Check out that API. I've got a bit more research to do. But it sounds like you can use that API function to swap string pointers instead of moving string values to memory locations. Should be a lot faster.
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  10. #10
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Array Sort

    Is there a native VBA way to sort arrays yet?

    Due to problems with this code in Office 2000, we checked the version before sorting:

    If Application.Version = "8.0" Then 'Word97 only
    WordBasic.SortArray (myArray())
    End If

  11. #11
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Array Sort

    Not a native way.

    What problems were there in the Word2000 Wordbasic.SortArray?

    Have you chec ked out Charlotte's example for a way to sort? (You no doubt have your own anyway)
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  12. #12
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Array Sort

    I think the WordBasic.SortArray method wasn't supported, at least in the templates I was creating. Not sure why.

    Actually, I don't have my own sort routine; somehow, the stuff miraculously turned out to be sorted when it displayed in the ListBox, so I never had to write one.

    Thanks for the link, though, you never know...

  13. #13
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Array Sort

    Jeff,

    I hate "miraculously". Sounds like something wrong.

    Was the listbox bound to a datasource- and hence sorted that way?
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  14. #14
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Array Sort

    >BTW, your pic is busted.

    No it's not. I attach the original ....

  15. #15
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Array Sort

    If the items were by chance, members of a collection, there seems to be some built-in sorting.
    Here's an example from something I'm working on right at the moment:

    If you go into a document and insert bookmarks around the first three paragraphs like so:

    Bookmark2 goes here
    Bookmark3 goes here
    Bookmark1 goes here

    And then run code like this:

    With lstBmkNames
    For n = 1 To objBmks.Count
    .AddItem objBmks(n)
    Next n
    End With

    What will display in the ListBox will be:

    Bookmark1
    Bookmark2
    Bookmark3

    So it seems that collections internally sort themselves by name, regardless of the order in which the items actually occur in the document.
    (You might have already known this one, but it was new to me! <g>)

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •