Jump to content

Sorting Dates in Arrays


wutzke
 Share

Recommended Posts

I have the start of a sorting script. It sorts the months ok, but changes the months and does not sort the year

#include <Array.au3>

Dim $avArray[10]
$avArray[0] = "Sep '06"
$avArray[1] = "Dec '05"
$avArray[2] = "Mar '05"
$avArray[3] = "Jun '04"
$avArray[4] = "Aug '06"
$avArray[5] = "Nov '05"
$avArray[6] = "Feb '05"
$avArray[7] = "May '04"
$avArray[8] = "Jul '05"
$avArray[9] = "Oct '06"

_ArrayDisplay( $avArray, "Unsorted" )

for $xx = 0 to 9
   $aNewDate=_toDate($avArray[$xx])
next

_ArraySort( $avArray)
_ArrayDisplay( $avArray, "Sort Ascending" )


Func _toDate($sDate)
    Global $A_Mon[13] = ["", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
$I_Date=$sDate
$a_Date = StringSplit($I_Date, " '")
$Mon = 0
For $x = 1 To 12
    If $a_Date[1] = $A_Mon[$x] Then
        $Mon = StringRight("0" & $x, 2)
        ExitLoop
    EndIf
Next
$newdate =  $Mon & "/" & "01" & "/" & $a_Date[3]
$avArray[$xx] = $newdate
EndFunc
Edited by wutzke
Link to comment
Share on other sites

  • Developers

could add the date in YYMM infront of the array value .... something like this:

#include <Array.au3>

Dim $avArray[10]
$avArray[0] = "Sep '06"
$avArray[1] = "Dec '05"
$avArray[2] = "Mar '05"
$avArray[3] = "Jun '04"
$avArray[4] = "Aug '06"
$avArray[5] = "Nov '05"
$avArray[6] = "Feb '05"
$avArray[7] = "May '04"
$avArray[8] = "Jul '05"
$avArray[9] = "Oct '06"

_ArrayDisplay($avArray, "Unsorted")

For $xx = 0 To 9
    _toDate($avArray[$xx])
Next

_ArraySort($avArray)
_ArrayDisplay($avArray, "Sort Ascending")


Func _toDate(ByRef $sDate)
    Global $A_Mon[13] = ["", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
    $I_Date = $sDate
    $a_Date = StringSplit($I_Date, " '")
    $Mon = 0
    For $x = 1 To 12
        If $a_Date[1] = $A_Mon[$x] Then
            $Mon = StringRight("0" & $x, 2)
            ExitLoop
        EndIf
    Next
    $sDate = $a_Date[3] & $Mon & "|" & $sDate
    Return 
EndFunc   ;==>_toDate

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Thanks,

that works.

But now say that I have only data for certain months and I what to sort that data, but with all the months including the one without data

#include <Array.au3>
#include <Date.au3>
#include<File.au3>

; format and fill into months
$myRptRun=12
$start_Date = "Sep '06"
$I_Date1 = $start_Date
$I_Date = $I_Date1
_toDate($I_Date)
    MsgBox(64, "report run:", $I_Date)





Dim $avArray[5]
$avArray[0] = "Sep '06|12"
$avArray[1] = "Dec '06|6"
$avArray[2] = "Mar '06|7"
$avArray[3] = "Jun '06|7"
$avArray[4] = "Aug '06|7"

_ArrayDisplay( $avArray, "" )

_fillinEmptyMonths()

Func _fillinEmptyMonths()
$I_months = $myRptRun+6
Dim $awArray[$I_months+1]
$awArray[0]=$I_Date1
for $z = 1 to $I_months
    $sNewDate = _DateAdd( 'M',-1, $I_Date)
    $I_Date = $sNewDate
$awArray[$z]=$sNewDate
Next
_ArrayDisplay( $awArray, "" )
EndFunc

Func _toDate(ByRef $sDate)
    Global $A_Mon[13] = ["", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
    $I_Date = $sDate
    $a_Date = StringSplit($I_Date, " '")
    $Mon = 0
    For $x = 1 To 12
        If $a_Date[1] = $A_Mon[$x] Then
            $Mon = StringRight("0" & $x, 2)
            ExitLoop
        EndIf
    Next
    $sDate = "20"  & $a_Date[3] & "/" & $Mon & "/01"
    Return
EndFunc  ;==>_toDate
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...