Thanzex Posted December 17, 2013 Share Posted December 17, 2013 Hi all! Sorry for my bad english. A few minutes ago i started coding the famous bubblesort algorithm. This is the code with a shitty GUI made just for input the values faster. expandcollapse popup#include <ButtonConstants.au3> #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <Array.au3> #Region ### START Koda GUI section ### Form= $Form2 = GUICreate("Form2", 405, 61, 302, 218) $insert = GUICtrlCreateInput("Insert", 16, 16, 121, 21) $inserted = GUICtrlCreateButton("inserted", 160, 16, 75, 25,$BS_DEFPUSHBUTTON) $ok = GUICtrlCreateButton("ok", 256, 16, 75, 25) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### $First=true While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $inserted if $First Then Global $Array[1]=[GUICtrlRead($insert)] GUICtrlSetData($insert,"") $First=False Else _ArrayAdd($Array,GUICtrlRead($insert)) GUICtrlSetData($insert,"") EndIf Case $ok Ordina() EndSwitch WEnd Func Ordina() $max=UBound($Array) While $max>0 For $i=0 To $max-2 If $Array[$i]>$Array[$i+1] Then _ArraySwap($Array[$i],$Array[$i+1]) EndIf Next $max=$max-1 WEnd _ArrayDisplay($Array) Exit EndFunc It all worked great until i noticed this error: i don't know how to explain it well in english, but the numbers are ordered in a strange way, this is a photo of the result: The script consider only the first 3 digits in this case, because the shortest number is made of three digits so it looks like: 123 124 125 127 It's not considering the real value of the number. Any ideas? Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted December 17, 2013 Moderators Share Posted December 17, 2013 Thanzex,Welcome to the AutoIt forum. As you can see the sort algorithm is treating the values as strings and sorting them on the ASCII values of the characters. Use Number to force the comparison to use their numeric value:If Number($Array[$i]) > Number($Array[$i + 1]) ThenPlease ask if you have any further questions. M23 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
ajag Posted December 17, 2013 Share Posted December 17, 2013 ...or replace the line _ArrayAdd($Array,GUICtrlRead($insert)) with _ArrayAdd($Array,Int(GUICtrlRead($insert))) A-Jay Rule #1: Always do a backup Rule #2: Always do a backup (backup of rule #1) Link to comment Share on other sites More sharing options...
DatMCEyeBall Posted December 17, 2013 Share Posted December 17, 2013 ...or replace the line _ArrayAdd($Array,GUICtrlRead($insert)) with _ArrayAdd($Array,Int(GUICtrlRead($insert))) A-Jay _ArrayAdd($Array, Number(GUICtrlRead($insert))) WIll allow for negative numbers. FlashpointBlack 1 "Just be fred, all we gotta do, just be fred." -Vocaliod "That is a Hadouken. A KAMEHAMEHA would have taken him 13 days and 54 episodes to form." - Roden Hoxha @tabhooked Clock made of cursors ♣ Desktop Widgets ♣ Water Simulation Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now