Library code snippets

Ascending to Descending sort

This demonstrates how to change an array from ascending order to descending order and vice versa.

Public Function AscDescOrder(varArray As Variant)
On Error GoTo EEvt
Dim I As Long, J As Long

'returns dates, numbers and names Descending
'the array must swap to Descending array
J = UBound(varArray)
I = 0

Do While J > I
   SwpElm varArray, I, J
   I = I + 1
   J = J - 1

   'count the progress too
   Form1.lblStatus.Caption = Format(I / J, "Percent")
Loop


Exit Function
EEvt:
MsgBox Err.Number & " " & Err.Description
Stop
Err.Clear
Resume Next
End Function


Function SwpElm(vItm As Variant, _
lngItmA As Long, lngItmB As Long)
Dim vTmp As Variant
   vTmp = vItm(lngItmB)
   vItm(lngItmB) = vItm(lngItmA)
   vItm(lngItmA) = vTmp
End Function

/html>

AddThis

Comments

  1. 15 Jul 2006 at 12:54

    Hello!

    Here is a slightly more efficient way.   

     

    For OUTY = 1 To Text1.Count

       Text1(OUTY).tag = 1

       For NUMBr = 1 To Text1.Count

          If Not OUTY = NUMBr Then

          If Text1(OUTY).Text > Text1(NUMBr).Text Then Text1(OUTY).tag = Text1(OUTY).tag + 1

          End If

       Next

    Next

    'and if you want an ordered output:

    For OUTY = 1 To Text1.Count

       For NUMBr = 1 To Text1.Count

          If Text1(numbr).tag = outy then lstOrdered.additem Text1(numbr).text

       Next

    Next

    the highest number/word score will get the most points (the total items) , if there is a tie then they both will be awarded the lesser of the points (unless you add another line in the middle). remember with vb,  < and > do work with alphabet. so it will work if you are doing words.
    text1.count is the total number of text1's (they have different indexes)
    if you want it back to front then change the greater than to a less than sign.

    Charlie.

Leave a comment

Sign in or Join us (it's free).

Related discussion