Nhato

simple _ArraySort drives me crazy

12 posts in this topic

Hello, sorry to bother you, i cannot find the mistake.

i have a .txt file containing:

407
772
613
363
775
Ultimate
840
1242
3520
1244

 

and i want to sort it with.

#Include <Array.au3>
#Include <File.au3>

Local $sort
_FileReadToArray(@workingdir & "\list.txt",$sort)
_ArrayDisplay($sort)
_ArraySort($sort,1,1)
_ArrayDisplay($sort)

 

but the result is this:

[0]|10
[1]|Ultimate
[2]|840
[3]|775
[4]|772
[5]|613
[6]|407
[7]|363
[8]|3520
[9]|1244
[10]|1242

i like that "Ultimate" is on top, but WHY are the 4 digit numbers at the bottom?

thanks for helping

Share this post


Link to post
Share on other sites



#Include <Array.au3>
Local $aList[10] = ["407", "772", "613", "363", "775", "Ultimate", "840", "1242", "3520", "1244"]
_ArrayDisplay($aList)

_ArraySort($aList, 1,1 )
_ArrayDisplay($aList)

The result is good : order descending by alphebitical order

Share this post


Link to post
Share on other sites

Don't forget you can do this in AuoIt v3.3.10.0+

Local $aList = ["407", "772", "613", "363", "775", "Ultimate", "840", "1242", "3520", "1244"]

 


_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 04/09/2015

Share this post


Link to post
Share on other sites

thanks for your help!

i also noticed that autoit sorts good when setting the array in the script, but i need to combine this with a text file that could grow very large. any ideas?

Share this post


Link to post
Share on other sites

Use a database, look at the SQLite functions. Having a flat file that grows exponentially over time is just going to cause more headaches than it's really worth. If you don't understand databases, then now is a good time as any to learn. Read this to see how to think in terms of tables etc..https://www.autoitscript.com/forum/topic/173843-databases-arent-scary-see-why-in-this-short-demo/


_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 04/09/2015

Share this post


Link to post
Share on other sites

....

but the result is this:

[0]|10
[1]|Ultimate
[2]|840
[3]|775
[4]|772
[5]|613
[6]|407
[7]|363
[8]|3520
[9]|1244
[10]|1242

i like that "Ultimate" is on top, but WHY are the 4 digit numbers at the bottom?

thanks for helping

That array sort above is correct - even though it's from a file. 

The array count,10,  is in the first element, put there by the _FileReadToArray function.

The 4 digit numbers at the bottom are treated as strings.  And are at the bottom because :-

3520 is less than 365 alphabetically sorted,

1244 is less than 3520 alphabetically sorted, and,

1242 is less than 1244 alphabetically sorted.

Here is an example of sorting an array numerically.

#include <Array.au3>
#include <File.au3>

Local $sort, $Temp, $Loop = 1
Local $Dec = 0 ; $Dec = 1 sort descending; $Dec = 0 sort ascending

_FileReadToArray("list.txt", $sort) ; $FRTA_COUNT (1) - array count in the first element. (default)
_ArrayDisplay($sort)

While $Loop ; Keep looping until If statement is missed.
    $Loop = 0
    For $i = 2 To UBound($sort) - 1
        If (Number($sort[$i]) > Number($sort[$i - 1]) And $Dec = 1) Or _      ; Sort Descending
                (Number($sort[$i]) < Number($sort[$i - 1]) And $Dec = 0) Then ; Sort Ascending

            ; ---- Swap $sort[$i] with $sort[$i - 1] ----
            $Temp = $sort[$i]
            $sort[$i] = $sort[$i - 1]
            $sort[$i - 1] = $Temp
            ; -------------------------------------------

            $Loop = 1 ; Keep looping
        EndIf
    Next
WEnd

_ArrayDisplay($sort)

 

1 person likes this

Share this post


Link to post
Share on other sites

thanks Malkey for the very clever script!

Share this post


Link to post
Share on other sites

Ah, help please?

I used this data:

Local $aList = ["407", "772", "613", "363", "775", "Ultimate", "840", "1242", "3520", "1244"]

using the sort function above, and got this result:

407
Ultimate
363
613
772
775
840
1242
1244
3520

Forgive my confusion, but wasnt this supposed to show a number sort?  ie I would expect "Ultimate" at the top, followed by all the numeric values, starting at 363, 407, 613... -> why does it (a) not place the text at the top (or bottom) and why are the numbers not in order?  Probably the same answer, but it is beyond me. 

Thank you for your kind assistance.

 


Skysnake

Why is the snake in the sky?

Share this post


Link to post
Share on other sites

Skysnake

Note that your sort does not include the first element in the sort.

The example sort of post #6, sorts the array from FileReadToArray function, which puts the number of elements of the array in the first element.

To allow the inclusion of the first element in the sort, change the line in the script of post #6 from

For $i = 2 To UBound($sort) - 1

to

For $i = 1 To UBound($sort) - 1

Share this post


Link to post
Share on other sites

my bad :(

sorted, thanks, works as expected


Skysnake

Why is the snake in the sky?

Share this post


Link to post
Share on other sites

You can use the Natural Order String Comparison by wraithdu.

#include "_ArrayNaturalSort.au3"

Local $aList[10] = ["407", "772", "613", "363", "775", "Ultimate", "840", "1242", "3520", "1244"]
_ArrayNaturalSort($aList)
_ArrayDisplay($aList)

 

Result:

Row|Col 0
[0]|363
[1]|407
[2]|613
[3]|772
[4]|775
[5]|840
[6]|1242
[7]|1244
[8]|3520
[9]|Ultimate

 


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 for the reference.  this has been a common question elsewhere on 'why does my x not sort like i expect' thanks, works well :)

 


Skysnake

Why is the snake in the sky?

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