Jump to content

Array sorting, a clarification


Recommended Posts

Hi guys,

I have a multiple array with some information, like name, address, date etc.

For example for the birth date, this is the correct ( and fastest, the main thing ) approach?

#include <Array.au3>

Global $aTEMP[15][2]

For $x = 1 To UBound($aTEMP, 1) - 1
    $aTEMP[$x][1] = StringFormat("%02i", Random(1,31,1)) & "/" & StringFormat("%02i", Random(1,12,1)) & "/" & Random(1960,2015,1)
    $aTEMP[$x][0] = $x
Next
_ArrayDisplay($aTEMP, "ORIGINAL")

Local $iDay, $iMonth, $iYear
For $x = 1 To UBound($aTEMP, 1) - 1
    $iDay = StringLeft($aTEMP[$x][1], 2)
    $iMonth = StringMid ($aTEMP[$x][1], 4, 2)
    $iYear = StringRight($aTEMP[$x][1], 4)
    $aTEMP[$x][1] = $iYear & $iMonth & $iDay
Next
_ArrayDisplay($aTEMP, "CONVERTED")
_ArraySort($aTEMP, 0, 1, 0, 1)
_ArrayDisplay($aTEMP, "SORTED")

For $x = 1 To UBound($aTEMP, 1) - 1
    $iDay = StringRight($aTEMP[$x][1], 2)
    $iMonth = StringMid($aTEMP[$x][1], 5, 2)
    $iYear = StringLeft($aTEMP[$x][1], 4)
    $aTEMP[$x][1] = $iDay & "/" & $iMonth & "/" & $iYear
Next
_ArrayDisplay($aTEMP, "FINAL")

If yes, for "numbers" i can use always the same method. If not, please provide a better example so i can study it.

Second question. If i have a list of file path in a file, how i can sort it in a correct way? I don't think i need to apply directly ArraySort, and yes is always a 2D array like before [number,path] There is an example for 2D path sorting?

Thanks for any help

Edited by Terenz

Nothing is so strong as gentleness. Nothing is so gentle as real strength

 

Link to comment
Share on other sites

  • Moderators

Terenz,

In my tests using StringSplit is significantly faster: :)

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

Global $aTEMP[1500][2]

For $x = 1 To UBound($aTEMP, 1) - 1
    $aTEMP[$x][1] = StringFormat("%02i", Random(1,31,1)) & "/" & StringFormat("%02i", Random(1,12,1)) & "/" & Random(1960,2015,1)
    $aTEMP[$x][0] = $x
Next

$aCopy = $aTEMP

$nBegin = TimerInit()
Local $iDay, $iMonth, $iYear
For $x = 1 To UBound($aTEMP, 1) - 1
    $iDay = StringLeft($aTEMP[$x][1], 2)
    $iMonth = StringMid ($aTEMP[$x][1], 4, 2)
    $iYear = StringRight($aTEMP[$x][1], 4)
    $aTEMP[$x][1] = $iYear & $iMonth & $iDay
Next
ConsoleWrite(TimerDiff($nBegin) & @CRLF)
_ArrayDisplay($aTEMP, "CONVERTED")

$nBegin = TimerInit()
For $i = 1 To UBound($aCopy) - 1
    $aSplit = StringSplit($aCopy[$i][1], "/")
    $aCopy[$i][1] = $aSplit[3] & $aSplit[2] & $aSplit[1]
Next
ConsoleWrite(TimerDiff($nBegin) & @CRLF)

_ArraySort($aTEMP, 0, 1, 0, 1)
_ArrayDisplay($aTEMP, "SORTED")
As to sorting numbers, as far as I know you need to get the content of the elements to be sorted into purely numerical format (and into a number datatype if there is a decimal point). ;)

As to the "path sorting" question - what do you mean by "correct way"? If you mean getting subfolders correctly placed beneath the parents as in a folder tree then the problem becomes quite complex - and there is an example of how this might be done in FileListToArrayRec. The inclusion of this sorting routine in the UDF led to a lot of unpleasantness, but if you want a properly ordered list you need to do a fair amount of work - as I found out when writing the sort routine. :D

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Mmm from my test is the opposite?

18.7792023846606
22.6850060552389

18.71047856641
19.7969294980228

18.1718626249984
18.7280785686449

18.9862119347571
20.0296406386845

18.0754816603786
16.6090941725834 ;<<<<<<<<<<<<<

18.7881420683355
20.629437540246

A single line seems also faster:

$aTEMP[$x][1] = StringRight($aTEMP[$x][1], 4) & StringMid ($aTEMP[$x][1], 4, 2) & StringLeft($aTEMP[$x][1], 2)
14.9655892019796
17.1309482071045

14.8172463259995
21.1283836353503

14.9063637976335
18.2785800988673

14.3839510328827
18.7459579359947

Only one time StringSplit was faster then mine, anyway are very very similar. For path, sorry but i don't have understand how to procede. I can't use FileListToArrayRec because i'l ready have a list and this list aren't file from my PC but from another one in LAN

Edited by Terenz

Nothing is so strong as gentleness. Nothing is so gentle as real strength

 

Link to comment
Share on other sites

  • Moderators

Terenz,

My tests always showed a significant speed advantage to the StringSplit code - particularly with larger arrays. ;)

I was not suggesting that you use FilelistToArrayRec, just informing you that there exists a sorting routine within that UDF to sort a list of paths into a hierarchical order - which might be of some use to you if that is what you were trying to do. It is not a trivial task (I blame that section of code for a lot of my grey hairs) and I would be delighted to help you try and code something if you wish. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

I have used your script from post #2 and i don't have change nothing, i have just run it more than once time and copy-paste the result of ConsoleWrite on this thread. Strange we have different result from the same script...

With:

Global $aTEMP[15000][2]

Result are:

153.76060365214
192.241472030663

154.84398156749
171.264504287556

154.063435436627
170.659678813927

I don't want to "win" ( i don't care, i want to use the faster version ) but i don't see the difference in favor of StringSplit, i see the opposite. Someone can confirm?

About the file path, maybe i need to use _RFLTA_ArraySort? That code is complex, I'm congratulate with you but i'll never script anything like that like in my life. But is for 1D array, i need for 2D...can you "improve" it? :D

Edited by Terenz

Nothing is so strong as gentleness. Nothing is so gentle as real strength

 

Link to comment
Share on other sites

  • Moderators

Terenz,

If you give me a copy of the list you want to sort and what you require as an output, I will see what I can do. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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