Jump to content

Sorting arrays by number


Recommended Posts

My problem is that I need to know which is the smalles number and then cont. to the largest number. What is happening is that the numbers are going by the digit and I need it to go to the whole number...does anyone know what I am doing wrong?

#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <array.au3>
Local $data[20][2], $data2[20]
Local $strintest = 0, $setit

$gui = GUICreate('', 260, 410)
$test = GUICtrlCreateCombo('', 0, 0, 200, 350, $WS_VSCROLL)
GUICtrlSetFont(-1, 10, Default, Default, "Consolas")

$but0 = GUICtrlCreateButton('col1', 200, 50)
$but1 = GUICtrlCreateButton('col2', 200, 150)
GUISetState()

For $i = 0 To UBound($data) - 1
    For $j = 0 To Random(3, 10)
        $data[$i][0] &= Random('1', '7', 1)
        $data[$i][1] &= Random('1', '7', 1)
    Next
Next

For $g = 0 To UBound($data) - 1
    If $strintest <= StringLen($data[$g][0]) Then $strintest = StringLen($data[$g][0])
Next

$strintest += 5

For $l = 0 To UBound($data) - 1
    If StringLen($data[$l][0]) <> $strintest Then
        Do
            $data[$l][0] &= ' '
        Until StringLen($data[$l][0]) >= $strintest
    EndIf
Next

_ArraySort($data, 0, 0, 0, 1)

For $i = 0 To UBound($data) - 1

    ConsoleWrite('!>' & $data[$i][0] & $data[$i][1] & @CRLF)
    $setit &= $data[$i][0] & $data[$i][1] & '|'
Next

GUICtrlSetData($test, $setit)

While 1
    $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then ExitLoop

    If $msg = $but0 Then
        ;MsgBox('','','')

        _ArraySort($data, 0, 0, 0, 0)
        $setit = ''
        For $i = 0 To UBound($data) - 1

            ConsoleWrite('!>' & $data[$i][0] & $data[$i][1] & @CRLF)
            If $data[$i][0] <> 0 Then $setit &= $data[$i][0] & $data[$i][1] & '|'
        Next
        GUICtrlSetData($test, '')
        GUICtrlSetData($test, $setit)

    EndIf

    If $msg = $but1 Then
        ;MsgBox('','','')
        $setit = ''
        _ArraySort($data, 0, 0, 0, 1)

        For $i = 0 To UBound($data) - 1

            ConsoleWrite('!>' & $data[$i][0] & $data[$i][1] & @CRLF)
            $setit &= $data[$i][0] & $data[$i][1] & '|'
        Next
        GUICtrlSetData($test, '')
        GUICtrlSetData($test, $setit)

    EndIf
WEnd

All by me:

"Sometimes you have to go back to where you started, to get to where you want to go." 

"Everybody catches up with everyone, eventually" 

"As you teach others, you are really teaching yourself."

From my dad

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

 

WindowsError.gif

WIKI | Tabs; | Arrays; | Strings | Wiki Arrays | How to ask a Question | Forum Search | FAQ | Tutorials | Original FAQ | ONLINE HELP | UDF's Wiki | AutoIt PDF

AutoIt Snippets | Multple Guis | Interrupting a running function | Another Send

StringRegExp | StringRegExp Help | RegEXTester | REG TUTOR | Reg TUTOT 2

AutoItSetOption | Macros | AutoIt Snippets | Wrapper | Autoit  Docs

SCITE | SciteJump | BB | MyTopics | Programming | UDFs | AutoIt 123 | UDFs Form | UDF

Learning to script | Tutorials | Documentation | IE.AU3 | Games? | FreeSoftware | Path_Online | Core Language

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace $ghGDIPDll with $__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

Ternary operator

Link to comment
Share on other sites

Look at the code for _ArraySort and you will notice something, if it finds a number it do a numerical comparison, otherwise it's just a string comparison. And what do you have?

For $i = 0 To UBound($data) - 1

    ConsoleWrite('!>' & $data[$i][0] & "/" & VarGetType($data[$i][0]) & @TAB & $data[$i][1] & @TAB & "/" & VarGetType($data[$i][1]) & @CRLF)
    $setit &= $data[$i][0] & $data[$i][1] & '|'
Next

Only strings... Good planning there! ;)

Link to comment
Share on other sites

Perhaps this is not entirely relevant here, but I thought it might be worth a mention. Sometimes I concatonate strings and numbers for various reasons (mainly to create numerical tags). Often when I do this, I prefix zeros to the numbers prior to concatonation, to avoid this kind of ambiguity. Maybe there are better ways to do this - I'm not sure.

Link to comment
Share on other sites

@czardas

Not sure what you mean.

The problem here is that nitekram is abusing that List-control by putting a bunch of spaces in the array. Numbers don't have spaces. Right?

@nitekram

Here's a situation where you should just let your imagination go free and you will quickly find a solution.

Three examples and my thought on them:

  • Move the spaces to a second array (errr no)
  • Generate the spaces when the list is to be filled (why waste cpu on that?)
  • Use a ListView and a Input instead (awesome!)
Link to comment
Share on other sites

  • Moderators

nitekram,

Or just add another element to your array to hold the spaces and make sure the numerical values are stored as Numbers. Look between the <<<<<<< lines. There are a few changes to some other lines as well, but they shoud be pretty obvious :) :

#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <array.au3>
Local $data[20][3], $data2[20]
Local $strintest = 0, $setit

$gui = GUICreate('', 260, 410)
$test = GUICtrlCreateCombo('', 0, 0, 200, 350, $WS_VSCROLL)
GUICtrlSetFont(-1, 10, Default, Default, "Consolas")

$but0 = GUICtrlCreateButton('col1', 200, 50)
$but1 = GUICtrlCreateButton('col2', 200, 150)
GUISetState()

For $i = 0 To UBound($data) - 1
    For $j = 0 To Random(3, 10)
        $data[$i][0] &= Random('1', '7', 1)
        $data[$i][2] &= Random('1', '7', 1)
    Next
Next

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
For $i = 0 To UBound($data) - 1
    $n = 9 - StringLen($data[$i][0])
    $data[$i][1] = "  "
    For $j = 0 To $n
        $data[$i][1] &= " "
    Next
    $data[$i][0] = Number($data[$i][0])
    $data[$i][2] = Number($data[$i][2])
Next
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

_ArraySort($data, 0, 0, 0, 1)

For $i = 0 To UBound($data) - 1

    ConsoleWrite('!>' & $data[$i][0] & $data[$i][1] & $data[$i][2] & @CRLF)
    $setit &= $data[$i][0] & $data[$i][1] & $data[$i][2] & '|'
Next

GUICtrlSetData($test, $setit)

While 1
    $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then ExitLoop

    If $msg = $but0 Then
        ;MsgBox('','','')

        _ArraySort($data, 0, 0, 0, 0)
        $setit = ''
        For $i = 0 To UBound($data) - 1

            ConsoleWrite('!>' & $data[$i][0] & $data[$i][1] & $data[$i][2] & @CRLF)
            If $data[$i][0] <> 0 Then $setit &= $data[$i][0] & $data[$i][1] & $data[$i][2] & '|'
        Next
        GUICtrlSetData($test, '')
        GUICtrlSetData($test, $setit)

    EndIf

    If $msg = $but1 Then
        ;MsgBox('','','')
        $setit = ''
        _ArraySort($data, 0, 0, 0, 2)

        For $i = 0 To UBound($data) - 1

            ConsoleWrite('!>' & $data[$i][0] & $data[$i][1] & $data[$i][2] & @CRLF)
            $setit &= $data[$i][0] & $data[$i][1] & $data[$i][2] & '|'
        Next
        GUICtrlSetData($test, '')
        GUICtrlSetData($test, $setit)

    EndIf
WEnd

Happy? ;)

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

Numbers don't have spaces. Right?

Right!

Not sure what you mean.

Suppose I find two identical strings in an array search. I may wish to log them separately: string001 and string018. While it normally has no significance where the strings were found, in one instance I needed to sort the strings in a completely controlled manner. So I added tags to ensure predictable behaviour. The reason had to do with the nature of the rest of the code. I hope that makes a little bit more sense. :)

So the sorted strings would end up being sorted first by alphabet, and then by the order in which they appeared: This had the additional bonus that the process could also be reversed. Perhaps that might seem strange, but it was just what I needed at the time. ;)

Edited by czardas
Link to comment
Share on other sites

Thanks for your help...again!

All by me:

"Sometimes you have to go back to where you started, to get to where you want to go." 

"Everybody catches up with everyone, eventually" 

"As you teach others, you are really teaching yourself."

From my dad

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

 

WindowsError.gif

WIKI | Tabs; | Arrays; | Strings | Wiki Arrays | How to ask a Question | Forum Search | FAQ | Tutorials | Original FAQ | ONLINE HELP | UDF's Wiki | AutoIt PDF

AutoIt Snippets | Multple Guis | Interrupting a running function | Another Send

StringRegExp | StringRegExp Help | RegEXTester | REG TUTOR | Reg TUTOT 2

AutoItSetOption | Macros | AutoIt Snippets | Wrapper | Autoit  Docs

SCITE | SciteJump | BB | MyTopics | Programming | UDFs | AutoIt 123 | UDFs Form | UDF

Learning to script | Tutorials | Documentation | IE.AU3 | Games? | FreeSoftware | Path_Online | Core Language

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace $ghGDIPDll with $__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

Ternary operator

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