Jump to content
Sign in to follow this  
buzz44

Duration Sorter?

Recommended Posts

Does any one have a 'Duration Sorter', one that can sort the time in H:M:S. Now your probably saying I can use _TimeToTicks () work out the highest number, and then use _TicksToTime () to convert it back. think is if its 0 hours 1 minute and 5 seconds it returns 0:1:5, where as I would like it to be 1:05.

I am using a variation of _ArraySort() to sort the minutes and hours ok, but when it gets to the less than one minute, so the format is 0:XX you have to like split the "0:" and then sort that number and then peice it back together.

I was wondering if anyone had any function(s) like this?


qq

Share this post


Link to post
Share on other sites

Try the following _Time_ArraySort. WARNING *not tested*

CODE
#include <Date.au3>

Func _Time_ArraySort(ByRef $a_Array, $i_Decending = 0, $i_Base = 0, $i_UBound = 0, $i_Dim = 1, $i_SortIndex = 0)

Local $A_Size, $Gap, $Count, $Temp, $C_Dim

Local $b_ExchangeValues = 0

Local $IsChanged = 0

; Set to ubound when not specified

If $i_UBound < 1 Then $i_UBound = UBound($a_Array) - 1

If UBound($a_Array) <= $i_UBound Or Not IsNumber($i_UBound) Then

SetError(1)

Return 0

EndIf

; Shell sort array

$A_Size = $i_UBound

$Gap = Int($A_Size / 2)

$b_ExchangeValues = 0

$IsChanged = 0

;

While $Gap <> 0

$IsChanged = 0

For $Count = $i_Base To ($A_Size - $Gap)

$b_ExchangeValues = 0

If $i_Dim = 1 Then

If $i_Decending <> 1 Then; sort array Ascending

If _Helper_TimeToTicks($a_Array[$Count]) > _Helper_TimeToTicks($a_Array[$Count + $Gap]) Then

$b_ExchangeValues = 1

EndIf

Else ; sort array Descending

If _Helper_TimeToTicks($a_Array[$Count]) < _Helper_TimeToTicks($a_Array[$Count + $Gap]) Then

$b_ExchangeValues = 1

EndIf

EndIf

If ($b_ExchangeValues) Then

$Temp = $a_Array[$Count]

$a_Array[$Count] = $a_Array[$Count + $Gap]

$a_Array[$Count + $Gap] = $Temp

$IsChanged = 1

EndIf

Else

If $i_Decending <> 1 Then; sort array Ascending

If _Helper_TimeToTicks($a_Array[$Count][$i_SortIndex]) > _Helper_TimeToTicks($a_Array[$Count + $Gap][$i_SortIndex]) Then

$b_ExchangeValues = 1

EndIf

Else ; sort array Descending

If _Helper_TimeToTicks($a_Array[$Count][$i_SortIndex]) < _Helper_TimeToTicks($a_Array[$Count + $Gap][$i_SortIndex]) Then

$b_ExchangeValues = 1

EndIf

EndIf

If ($b_ExchangeValues) Then

For $C_Dim = 0 To $i_Dim - 1

$Temp = $a_Array[$Count][$C_Dim]

$a_Array[$Count][$C_Dim] = $a_Array[$Count + $Gap][$C_Dim]

$a_Array[$Count + $Gap][$C_Dim] = $Temp

$IsChanged = 1

Next

EndIf

EndIf

Next

; If no changes were made to array, decrease $gap size

If $IsChanged = 0 Then

$Gap = Int($Gap / 2)

EndIf

WEnd

Return 1

EndFunc ;==>_ArraySort

Func _Helper_TimeToTicks($arg)

Local $hours, $mins, $secs, $i

Local $values = StringSplit($arg, ":")

If $values[0] = 2 Then

$hours = 0

$mins = $values[1]

$secs = $values[2]

ElseIf $values[0] = 3 Then

$hours = $values[1]

$mins = $values[2]

$secs = $values[3]

EndIf

Return _TimeToTicks($hours, $mins, $secs)

EndFunc


Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig!

Share this post


Link to post
Share on other sites

First impressions... Nice! I haven't fully tested tested this but its working fine so far :). When did you get it from? Own creation?

Edit: Would you also have one that would convert 0:1:5 to 1:05? :D. It might be 0:1:05, I forget.

Edited by Burrup

qq

Share this post


Link to post
Share on other sites

I just modified the _ArraySort function so sort on a different criterion.

Currently, _ArraySort compares the direct elements of the arrays. For example, If $array[$j] > $array[$k] Then

I told it to compare _Helper_TimeToTicks($array[$j]) > _Helper_TimeToTicks($array[$k])

Replace _Helper_TimeToTicks with any function you want. It could be StringLength if you wanted to sort by number of characters instead of alphabetical. You could choose Sin or Cos or whatever.

_Helper_TimeToTicks, by the way, simply calls _TimeToTicks with the proper arguments.

_TimeToTicks($array[$j]) would not work because _TimeToTicks requires three paramters, so I use the helper function to separate the parts.


Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig!

Share this post


Link to post
Share on other sites

Edit: Would you also have one that would convert 0:1:5 to 1:05? :). It might be 0:1:05, I forget.

<{POST_SNAPBACK}>

Maybe:

Func _convert($arg)
    If StringLeft($arg, 2) = "0:" Then $arg = StringTrimLeft($arg, 2)
    Local $colonPos = StringInStr($arg, ":", 0, -1);count backwards
    If Number(StringMid($arg,$colonPos+1)) <= 9 Then $arg = StringLeft($arg, $colonPos) & "0" & StringMid($arg, $colonPos+1)
    Return $arg
EndFunc
Edited by CyberSlug

Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig!

Share this post


Link to post
Share on other sites

Nice, I looked at the code but I didnt realise it was that similiar, cause it is, to _ArraySort(). Maybe you can hekp me again lol.

I trying to sort this type of information with the function you provided me.

"TT:TT|XXXXXX|XXX|XXXXXXX|XX"

Where "TT:TT" is the time, eg, "1:05" or "1:21:05" etc. "XXXX|XXX" is information attateched to the time. Infact the time is the length of a song and the XX|XX etc is all the information about it, Title, Artist etc. So the correct time needs to be associated witht he correct information.

Can I modify your version to sort the numbers only up to the first "|", which I am using to split the information. Otherwise I have to created 2 arrays, split the Time and information up and give it the same element, then sort using the above function, and then combine then use $var1[$i] &= $var2[$i] or something similiar to put it back to the original format of "TT:TT|XXXXXX|XXX|XXXXXXX|XX" but this time it will be sorted.

Question again: Can I modify the above fuction to do this or can you think of a better way to do this then I have stated above.

Thanks in advance, Burrup

Edit: Dont it again CyberSlug. _Convert() Works fine. Thanks

Edited by Burrup

qq

Share this post


Link to post
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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...