Jump to content

Sorting an Array


cgasb
 Share

Recommended Posts

I am doing some work in an Access database using the ADO UDF. Using the function _accessQueryLike returns an array where the values are seperated by Chr(28). Ex:

$aHuntCSR = _accessQueryLike($sFilePath, $sWMTable, $sWGDN, $iCSR)
 _ArrayDisplay($aHuntCSR, "")

returns this:

[0]|11

[1]|6704108472

[2]|670402029

[3]|670412030

[4]|670422031

[5]|670432032

[6]|670442033

[7]|670467006

[8]|670472047

[9]|670487009

[10]|670497068

[11]|6704117012

I am having a time figuring a good way to sort by the second column (determing the largest value would also work for me). The one with 10,0,1,2,3 etc. Is there a way to sort this type of array like that? Many thanks for the help!

Edited by cgasb
Link to comment
Share on other sites

I am doing some work in an Access database using the ADO UDF. Using the function _accessQueryLike returns an array where the values are seperated by Chr(28). Ex:

$aHuntCSR = _accessQueryLike($sFilePath, $sWMTable, $sWGDN, $iCSR)
 _ArrayDisplay($aHuntCSR, "")

returns this:

[0]|11

[1]|6704108472

[2]|670402029

[3]|670412030

[4]|670422031

[5]|670432032

[6]|670442033

[7]|670467006

[8]|670472047

[9]|670487009

[10]|670497068

[11]|6704117012

I am having a time figuring a good way to sort by the second column (determing the largest value would also work for me). The one with 10,0,1,2,3 etc. Is there a way to sort this type of array like that? Many thanks for the help!

You could StringSplit Each of the array Elements Using Chr(28) Then Sort the resolting array. In the meantime I'll take a look at adding a sort parameter into the function. I'm sure that if I do that then the next request will be for sort on multiple colums so I might as well take that as a given right now.

I would probably change the function call by adding in $rSort = -1 so as not to break existing scripts. And I'll probably expect $rSort to be an array if it's not -1. That whole UDF has already been changed. My copy doesn't even use those function names any more and the name of the UDF has been changed. Expect an update in the next few days.

EDIT: Just had a second thought about this. In your case you are only using numbers so the problem is more one of leading 0's being dropped or not used at all. That I could quickly fix using StringLen and IsNumber but I still think the best way will be to do an actual sort, even then I may run into the same leading 0 issue.

For now What about

For $I = 1 To Ubound($Array) -1
   For $J = 0 To 9
      $sString = Chr(28) & $J & Chr(28)
      $rString = Chr(28) & "0" & $J & Chr(28)
      If StringInStr($Array[$I], $sString) Then $Array[$I] = StringReplace($Array[$I], $sString, $rString)
   Next
Next
_ArraySort($array, 0, 1)
This is untested but I think it's right. Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Some version of this should work for that:

Global $iMaxIndex = 0, $iMaxVal = ""
For $n = 1 to $aHuntCSR[0]
    $avTemp = StringSplit($aHuntCSR[$n], Chr(28))
    If $avTemp[0] >= 2 Then
        If Number($avTemp[2]) > $iMaxVal Then 
            $iMaxIndex = $n
            $iMaxVal = $avTemp[2]
        EndIf
    EndIf
Next

If $iMaxIndex Then
    MsgBox(64, "Max Found", "The max value was " & $iMaxVal & ", found at index " & $iMaxIndex)
Else
    MsgBox(16, "Error", "Ivalid search results")
EndIf

It's just splitting each element of the array to test it.

:)

Edit: ... which GEOSoft already mentioned... Doh!

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

You could StringSplit Each of the array Elements Using Chr(28) Then Sort the resolting array. In the meantime I'll take a look at adding a sort parameter into the function. I'm sure that if I do that then the next request will be for sort on multiple colums so I might as well take that as a given right now.

I would probably change the function call by adding in $rSort = -1 so as not to break existing scripts. And I'll probably expect $rSort to be an array if it's not -1. That whole UDF has already been changed. My copy doesn't even use those function names any more and the name of the UDF has been changed. Expect an update in the next few days.

Thanks for the tip. I'll head down the StringSplit road and look forward to checking out the updates.

Link to comment
Share on other sites

Thanks for the tip. I'll head down the StringSplit road and look forward to checking out the updates.

Read my edit first.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

That worked! Thanks so much for the help.

No problem. Glad it worked.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Hi,

Sorting arrays of strings is faster using my Subsort UDF, in Array2D in link from my signature; [but eliminate first row, and each line must have the same nmber of elements/ separators ("|")]

Best, Randall

_Array2D1DFieldSortSt(ByRef $arEither2D1D, $iIndex = "1"); 1D must be "|" delimited!
Link to comment
Share on other sites

Hi,

Sorting arrays of strings is faster using my Subsort UDF, in Array2D in link from my signature; [but eliminate first row, and each line must have the same nmber of elements/ separators ("|")]

Best, Randall

_Array2D1DFieldSortSt(ByRef $arEither2D1D, $iIndex = "1"); 1D must be "|" delimited!
I think the problem of dropped or missing leading zeros will persist randall

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

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...