Jump to content

simple _ArraySort drives me crazy


Recommended Posts

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

Link to comment
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

Link to comment
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"]

 

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_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: 22/04/2018

Link to comment
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/

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_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: 22/04/2018

Link to comment
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)

 

Link to comment
Share on other sites

  • 2 weeks later...

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?

Link to comment
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
Link to comment
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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

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