Jump to content

String sorting - Date and time


Recommended Posts

Hi everybody,

i have a question related to strings items in an Array and sorting. Maybe someone can advice me how to solve the issue.

I have an Array of strings, every item of the Array is as following:

  • INFO [13.06.2017 11:48:01] [Thread-13] [ConGenImpUsb -> waitForConnection] 
  • INFO [07.06.2017 08:55:44] [main] MDU5 - Ver 5.1x

I want to sort the item in the array by date and time, is there any function which allows me to sort by date/time?

Thanks in advance

Link to comment
Share on other sites

Basic example, although someone that is good with RegEx could write something better.

#include <Array.au3>
Local $aInfo[2] = ['INFO [07.06.2017 11:48:01] [Thread-13] [ConGenImpUsb -> waitForConnection]', 'INFO [07.06.2017 08:55:44] [main] MDU5 - Ver 5.1x']
Local $aModified[UBound($aInfo)][2], $sDate
For $i = 0 To UBound($aInfo) - 1
    $sDate = StringMid($aInfo[$i], StringInStr($aInfo[$i], "INFO [") + StringLen("INFO ["), 19)
    $aModified[$i][0] = StringFormat("%04i/%02i/%02i", StringMid($sDate, 7, 4), StringMid($sDate, 4, 2), StringLeft($sDate, 2)) & StringRight($sDate, 9)
    $aModified[$i][1] = $aInfo[$i]
Next
_ArraySort($aModified, 0, 0, 0, 0)
_ArrayDisplay($aModified)

 

Link to comment
Share on other sites

another way.

 

#include <Array.au3>
Local $aArray = ["INFO [13.06.2017 11:48:01] [Thread-13] [ConGenImpUsb -> waitForConnection]", _
        "INFO [07.06.2017 08:55:44] [main] MDU5 - Ver 5.1x", _
        "INFO [07.06.2017 12:55:11] [main] Dummy String1", _
        "INFO [07.06.2016 09:55:11] [main] Dummy String2", _
        "INFO [07.06.2017 09:55:12] [main] Dummy String3", _
        "INFO [07.06.2017 09:55:11] [main] Dummy String4"]


_ArrayDisplay($aArray, "No sorted")
_Sort($aArray)
_ArrayDisplay($aArray, "Sorted")

Func _Sort(ByRef $aArray)
    For $i = UBound($aArray) - 1 To 1 Step -1
        For $j = 1 To $i
            If _GetNumber($aArray[$j - 1]) > _GetNumber($aArray[$j]) Then
                $temp = $aArray[$j - 1]
                $aArray[$j - 1] = $aArray[$j]
                $aArray[$j] = $temp
            EndIf
        Next
    Next
    Return $aArray
EndFunc   ;==>_Sort


Func _GetNumber($String)
    Return Number(StringRegExpReplace(StringMid($String, 7, 19), '(\d{2})\.(\d{2})\.(\d{4})', "$3$2$1")) ;mikell's regexp
EndFunc   ;==>_GetNumber

Saludos

Edited by Danyfirex
Link to comment
Share on other sites

So assuming you have that portion of the string, since that's been done twice now,  here is a way with datediff

#include <Date.au3>
#include<array.au3>

local $Arr[5]
$Arr[0] = "13.06.2017 11:48:01"
$Arr[1] = "07.06.2017 08:55:44"
$Arr[2] = "05.06.2017 08:55:40"
$Arr[3] = "12.06.2017 08:55:43"
$Arr[4] = "07.06.2017 08:55:42"

For $i = 0 to ubound($Arr) - 2

    If stringleft( _DateDiff("s" , stringmid($Arr[$i] , 7 , 4) & "/" & stringmid($Arr[$i] , 4 , 2) & "/" &  stringmid($Arr[$i] , 1 , 2) & " " & stringright($Arr[$i] , 8) , stringmid($Arr[$i + 1] , 7 , 4) & "/" & stringmid($Arr[$i + 1] , 4 , 2) & "/" &  stringmid($Arr[$i + 1] , 1 , 2) & " " & stringright($Arr[$i + 1] , 8)) , 1) = "-" Then
        _ArraySwap($Arr , $i , $i + 1)
        $i = -1
    EndIf

Next

_ArrayDisplay($Arr)

 

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

And another way ... sooo classic   :)

#include <Array.au3>

Local $aArray[6] = ["INFO [13.06.2017 11:48:01] [Thread-13] [ConGenImpUsb -> waitForConnection]", _
        "INFO [07.06.2018 08:55:44] [main] MDU5 - Ver 5.1x", _
        "INFO [07.06.2017 12:55:11] [main] Dummy String1", _
        "INFO [07.06.2016 09:55:11] [main] Dummy String2", _
        "INFO [07.06.2017 09:55:12] [main] Dummy String3", _
        "INFO [07.06.2017 09:55:11] [main] Dummy String4"]

_ArrayDisplay($aArray, "No sorted")
_Sort($aArray)
_ArrayDisplay($aArray, "Sorted")


Func _Sort(ByRef $aArray)
     _ArrayColInsert($aArray, 1)
    For $i = 0 To UBound($aArray) - 1
       $aArray[$i][1] = StringRegExpReplace($aArray[$i][0], '(\d{2})\.(\d{2})\.(\d{4})', "$3$2$1")
    Next
    _ArraySort($aArray, 0, 0, 0, 1)
    _ArrayColDelete($aArray, 1)
EndFunc   ;==>_Sort

 

Link to comment
Share on other sites

This solution worked greatly. Many thanks.

 

I didn't try yet the other solutions

 

 

On ‎23‎.‎06‎.‎2017 at 6:39 PM, Danyfirex said:

another way.

 

#include <Array.au3>
Local $aArray = ["INFO [13.06.2017 11:48:01] [Thread-13] [ConGenImpUsb -> waitForConnection]", _
        "INFO [07.06.2017 08:55:44] [main] MDU5 - Ver 5.1x", _
        "INFO [07.06.2017 12:55:11] [main] Dummy String1", _
        "INFO [07.06.2016 09:55:11] [main] Dummy String2", _
        "INFO [07.06.2017 09:55:12] [main] Dummy String3", _
        "INFO [07.06.2017 09:55:11] [main] Dummy String4"]


_ArrayDisplay($aArray, "No sorted")
_Sort($aArray)
_ArrayDisplay($aArray, "Sorted")

Func _Sort(ByRef $aArray)
    For $i = UBound($aArray) - 1 To 1 Step -1
        For $j = 1 To $i
            If _GetNumber($aArray[$j - 1]) > _GetNumber($aArray[$j]) Then
                $temp = $aArray[$j - 1]
                $aArray[$j - 1] = $aArray[$j]
                $aArray[$j] = $temp
            EndIf
        Next
    Next
    Return $aArray
EndFunc   ;==>_Sort


Func _GetNumber($String)
    Return Number(StringRegExpReplace(StringMid($String, 7, 19), "\D", ""))
EndFunc   ;==>_GetNumber

Saludos

 

Link to comment
Share on other sites

thank you mikell :-P Fixed first reply.

 

 

Saludos

Edited by Danyfirex
Link to comment
Share on other sites

Link to comment
Share on other sites

1 hour ago, Subz said:

Can you post the more complex file?

 

Unfortunately i cannot post my original file, because it contains many entries with sensitive data. Anyway there are several entries like the one i posted before. The log content is just bigger.

 

As for now i am using this solution and i will test it for a while:

 

On ‎23‎.‎06‎.‎2017 at 9:49 PM, mikell said:

And another way ... sooo classic   :)

#include <Array.au3>

Local $aArray[6] = ["INFO [13.06.2017 11:48:01] [Thread-13] [ConGenImpUsb -> waitForConnection]", _
        "INFO [07.06.2018 08:55:44] [main] MDU5 - Ver 5.1x", _
        "INFO [07.06.2017 12:55:11] [main] Dummy String1", _
        "INFO [07.06.2016 09:55:11] [main] Dummy String2", _
        "INFO [07.06.2017 09:55:12] [main] Dummy String3", _
        "INFO [07.06.2017 09:55:11] [main] Dummy String4"]

_ArrayDisplay($aArray, "No sorted")
_Sort($aArray)
_ArrayDisplay($aArray, "Sorted")


Func _Sort(ByRef $aArray)
     _ArrayColInsert($aArray, 1)
    For $i = 0 To UBound($aArray) - 1
       $aArray[$i][1] = StringRegExpReplace($aArray[$i][0], '(\d{2})\.(\d{2})\.(\d{4})', "$3$2$1")
    Next
    _ArraySort($aArray, 0, 0, 0, 1)
    _ArrayColDelete($aArray, 1)
EndFunc   ;==>_Sort

 

 

I have just a problem, i have a monodimensional Array in Input and it is basically "converted" to bidimensional. How can i use the array? I mean, how can i access the array items?

 

Link to comment
Share on other sites

Basic example:

#include <Array.au3>

Global $aOneDimension[3] = [2, "Row1", "Row2"]
_ArrayDisplay($aOneDimension)
For $i = 1 To $aOneDimension[0]
    MsgBox(4096, "One Dimension Array", $aOneDimension[$i])
Next

Global $aTwoDimension[3][3] = [["Row0 Col0", "Row0 Col1", "Row0 Col2"],["Row1 Col0", "Row1 Col1", "Row1 Col2"], ["Row2 Col0", "Row2 Col1", "Row2 Col2"]]
_ArrayDisplay($aTwoDimension)
For $i = 0 To UBound($aTwoDimension) - 1
    MsgBox(4096, "Two Dimension Array", "Row" & $i & @CRLF & $aTwoDimension[$i][0] & @TAB & $aTwoDimension[$i][1] & @TAB & $aTwoDimension[$i][2])
Next

 

Link to comment
Share on other sites

Hello. Here is my script fixed.

 

#include <Array.au3>

Local $aArray[6] = ["INFO [13.06.2017 11:48:01] [Thread-13] [ConGenImpUsb -> waitForConnection]", _
        "INFO [07.06.2018 08:55:44] [main] MDU5 - Ver 5.1x", _
        "INFO [07.06.2017 12:55:11] [main] Dummy String1", _
        "INFO [07.06.2016 09:55:11] [main] Dummy String2", _
        "INFO [07.06.2017 09:55:12] [main] Dummy String3", _
        "INFO [07.06.2017 09:55:11] [main] Dummy String4"]


_ArrayDisplay($aArray, "No sorted")
_Sort($aArray)
_ArrayDisplay($aArray, "Sorted")



Func _Sort(ByRef $aArray)
    For $i = UBound($aArray) - 1 To 1 Step -1
        For $j = 1 To $i
            If _GetNumber($aArray[$j - 1]) > _GetNumber($aArray[$j]) Then
                $temp = $aArray[$j - 1]
                $aArray[$j - 1] = $aArray[$j]
                $aArray[$j] = $temp
            EndIf
        Next
    Next
    Return $aArray
EndFunc   ;==>_Sort


Func _GetNumber($String)
    Local $iNumber=Number(StringRegExpReplace(StringRegExpReplace(StringMid($String, 7, 19), '(\d{2})\.(\d{2})\.(\d{4})', "$3$2$1"),"\D","")) ;mikell's regexp plus something
    Return $iNumber
EndFunc   ;==>_GetNumber

 

Saludos

Link to comment
Share on other sites

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

Local $aArray[6] = ["INFO [13.06.2017 11:48:01] [Thread-13] [ConGenImpUsb -> waitForConnection]", _
"INFO [07.06.2018 08:55:44] [main] MDU5 - Ver 5.1x", _
"INFO [07.06.2017 12:55:11] [main] Dummy String1", _
"INFO [07.06.2016 09:55:11] [main] Dummy String2", _
"INFO [07.06.2017 09:55:12] [main] Dummy String3", _
"INFO [07.06.2017 09:55:11] [main] Dummy String4"]


For $i = 0 to ubound($aArray) - 2

    If stringleft( _DateDiff("s" , stringmid(StringRegExp($aArray[$i] , '(\d{2}\.\d{2}\.\d{4}.\d{2}:\d{2}:\d{2})' , 3)[0] , 7 , 4) & _
        "/" & stringmid(StringRegExp($aArray[$i] , '(\d{2}\.\d{2}\.\d{4}.\d{2}:\d{2}:\d{2})' , 3)[0] , 4 , 2) & _
        "/" & stringmid(StringRegExp($aArray[$i] , '(\d{2}\.\d{2}\.\d{4}.\d{2}:\d{2}:\d{2})' , 3)[0] , 1 , 2) & _
        " " & stringright(StringRegExp($aArray[$i] , '(\d{2}\.\d{2}\.\d{4}.\d{2}:\d{2}:\d{2})' , 3)[0] , 8) , stringmid(StringRegExp($aArray[$i + 1] , '(\d{2}\.\d{2}\.\d{4}.\d{2}:\d{2}:\d{2})' , 3)[0] , 7 , 4) & _
        "/" & stringmid(StringRegExp($aArray[$i + 1] , '(\d{2}\.\d{2}\.\d{4}.\d{2}:\d{2}:\d{2})' , 3)[0] , 4 , 2) & _
        "/" & stringmid(StringRegExp($aArray[$i + 1] , '(\d{2}\.\d{2}\.\d{4}.\d{2}:\d{2}:\d{2})' , 3)[0] , 1 , 2) & " " & stringright(StringRegExp($aArray[$i+1] , '(\d{2}\.\d{2}\.\d{4}.\d{2}:\d{2}:\d{2})' , 3)[0] , 8)) , 1) = "-" Then

            _ArraySwap($aArray , $i , $i + 1)
            $i = -1
    EndIf

Next

_ArrayDisplay($aArray)

 

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

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

×
×
  • Create New...