Results 1 to 2 of 2

Thread: Sorting something by date!!!

  1. #1
    Guest
    I've got an array containing dates in one list and cash values in t'other. I need it soting into acsending date order, relative to each dates' cash value.

    Does anyone know how to do this?

  2. #2
    Fanatic Member
    Join Date
    Mar 2000
    Location
    That posh bit of England known as Buckinghamshire
    Posts
    658

    Sorting Dates

    First. Make sure the date is in the format yyyymmdd. So the year comes first followed by the month and then the day.

    Once the date is in this fromat you can simply call a sorting algorithim to sort the dates for you.

    As an example here is a quick sort algorithim. You pass it an array of data (in this eg Strings) and the lower bound (usually 0) and the upper bound (the number of elements in the list)

    Code:
    ' Sort the items in array values() with bounds min and max.
    Sub Quicksort(values() As String, _
                  ByVal min As Long, _
                  ByVal max As Long)
    
      Dim med_value As String
      Dim hi As Long
      Dim lo As Long
      Dim i As Long
    
      ' If the list has only 1 item, it's sorted.
      If min >= max Then Exit Sub
    
      ' Pick a dividing item randomly.
      i = min + Int(Rnd(max - min + 1))
      med_value = values(i)
    
      ' Swap the dividing item to the front of the list.
      values(i) = values(min)
    
      ' Separate the list into sublists.
      lo = min
      hi = max
      Do
        ' Look down from hi for a value < med_value.
        Do While values(hi) >= med_value
          hi = hi - 1
          If hi <= lo Then Exit Do
        Loop
    
        If hi <= lo Then
          ' The list is separated.
          values(lo) = med_value
          Exit Do
        End If
    
        ' Swap the lo and hi values.
        values(lo) = values(hi)
    
        ' Look up from lo for a value >= med_value.
        lo = lo + 1
        Do While values(lo) < med_value
          lo = lo + 1
          If lo >= hi Then Exit Do
        Loop
    
        If lo >= hi Then
          ' The list is separated.
          lo = hi
          values(hi) = med_value
          Exit Do
        End If
    
        ' Swap the lo and hi values.
        values(hi) = values(lo)
      Loop ' Loop until the list is separated.
    
      ' Recursively sort the sublists.
      Quicksort values, min, lo - 1
      Quicksort values, lo + 1, max
    
    End Sub
    Iain.

    [Edited by Iain17 on 04-10-2000 at 04:37 PM]

Posting Permissions

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



Click Here to Expand Forum to Full Width