Sign in to follow this  
Followers 0
c0re

Issues sorting array with numbers and text

3 posts in this topic

Hi everyone,

I'm having some trouble trying to sort a simple array containing a number, a delimiter, and some text. Example - "183942 : data". In my test defined array it works correctly, but when I use _FileListToArray to read in directory names and DirGetSize for the number _ArraySort does not sort as expected. I thought there might be some weird white space or a CR in the element so I tried using StringStripWS and StringStripCR with no success. I do a round when I put the number and text in the array so I thought that might be an issue, I rounded first and then put in array with the same results.

I am expecting the array to be sorted from lowest to highest numerically. I'm sure I am just not understanding how _ArraySort is supposed to work, please let me know if you have any advice.

This is part of a script that scans a volume, if total disk used in >90% then will grab the size of each directory, the data owners of the top 5 directories on the disk, and email them a note asking to clean up their data. If you know of a existing script that does this please point me to it. ;)

Here is some example code that points to c:\program files and tries to sort the directory sizes with names. This example demonstrates sorting numerically does not work as I am expecting.

#Include <Array.au3>
#include <file.au3>
Dim $aDirs[1]
Dim $aDirsizeonly[1]
Dim $x, $dir, $aMixed, $aNumbers

$aMixed = _ArrayCreate("101: text", "1: text", "9224: text", "4239: text", "0: text", "1247: text")
$aNumbers = _ArrayCreate("101", "1", "9224", "4239", "0", "1247")

$dir = _FileListToArray('c:\program files', "*", 2)
If @Error=1 Then
    MsgBox (16,"Error","No Folders Found.")
    Exit
EndIf

For $x = 0 to UBound($dir)-1
    $size = DirGetSize('c:\program files\' & $dir[$x], 1)
    If IsArray($size) Then
        ;$size1 = Round($size[0] /1024/1024)
        ;_ArrayAdd($aDirs, $size1 & ' : ' & $dir[$x])
        _ArrayAdd($aDirsizeonly, Round($size[0] /1024/1024, 0))
        _ArrayAdd($aDirs, Round($size[0] /1024/1024, 0) & ' : ' & $dir[$x])
    EndIf
Next

_ArraySort($aNumbers)
_ArrayDisplay($aNumbers, "Defined array - Numbers only. This Works!")

_ArraySort($aMixed)
_ArrayDisplay($aMixed, "Defined array - Mixed text with Numbers. This Works!")

_ArraySort($aDirsizeonly)
_ArrayDisplay($aDirsizeonly,"DirGetSize array - Dir size only. This Works!")

_ArraySort($aDirs)
_ArrayDisplay($aDirs,"DirGetSize array - Dir name with size. This Does Not Sort As Expected.")

Thanks in advance!

Share this post


Link to post
Share on other sites



Look here: http://www.autoitscript.com/forum/index.php?showtopic=83626 and you will find the answer!

Br,

UEZ


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

Thanks UEZ! This worked as expected. :)

After dropping those into my includes dir and using _ArrayNaturalSort($aDirs) the resultant array was sorted as expected.

I still think it's odd that when I setup a defined test array in the "1234567 : text" format it's able to be sorted with _ArraySort, but if the array is populated in the same format but with data from DirGetSize and _FileListToArray it does not work. ;)

Thanks again!

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  
Followers 0