Jump to content

Sorting Numbers


Recommended Posts

So yeah... i'm trying to make a script that gives me medians... but I can't sort a list of numbers because it will put multiple-digit numbers in the wrong place.

For example, I put in 1,2,3,10,22,31 in _ArraySort and it gives me

1,10,2,22,3,31

Any way I can put these numbers in order of least to greatest?

[center]"When you look at old, classic games like Snake, you often put it off because it's such a simple game, but it's only when you actually try and create your own unique game from scratch, do you finally appreciate those games."[/center][center]Don't ask for answers if you haven't TRIED yet![/center][center]Most answers can be answered in the help file! Use it![/center]

Link to comment
Share on other sites

  • Moderators

So yeah... i'm trying to make a script that gives me medians... but I can't sort a list of numbers because it will put multiple-digit numbers in the wrong place.

For example, I put in 1,2,3,10,22,31 in _ArraySort and it gives me

1,10,2,22,3,31

Any way I can put these numbers in order of least to greatest?

Func _ArraySortNum(ByRef $n_array, $i_descending = 0, $i_start = 1)
    Local $i_ub = UBound($n_array)
    For $i_count = $i_start To $i_ub - 2
        Local $i_se = $i_count
        If $i_descending Then
            For $x_count = $i_count To $i_ub - 1
                If Number($n_array[$i_se]) < Number($n_array[$x_count]) Then $i_se = $x_count
            Next
        Else
            For $x_count = $i_count To $i_ub - 1
                If Number($n_array[$i_se]) > Number($n_array[$x_count]) Then $i_se = $x_count
            Next
        EndIf
        Local $i_hld = $n_array[$i_count]
        $n_array[$i_count] = $n_array[$i_se]
        $n_array[$i_se] = $i_hld
    Next
EndFunc

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

  • 4 months later...

Func _ArraySortNum(ByRef $n_array, $i_descending = 0, $i_start = 1)
    Local $i_ub = UBound($n_array)
    For $i_count = $i_start To $i_ub - 2
        Local $i_se = $i_count
        If $i_descending Then
            For $x_count = $i_count To $i_ub - 1
                If Number($n_array[$i_se]) < Number($n_array[$x_count]) Then $i_se = $x_count
            Next
        Else
            For $x_count = $i_count To $i_ub - 1
                If Number($n_array[$i_se]) > Number($n_array[$x_count]) Then $i_se = $x_count
            Next
        EndIf
        Local $i_hld = $n_array[$i_count]
        $n_array[$i_count] = $n_array[$i_se]
        $n_array[$i_se] = $i_hld
    Next
EndFunc

Hi

How do we use this function ? :)

Thanks in advance

Qui ose gagneWho Dares Win[left]CyberExploit[/left]

Link to comment
Share on other sites

Like This...

#include <array.au3>

$Array = StringSplit("2,5,3,4,6,1,8,9,7", ",")

_ArraySortNum($Array)

_ArrayDisplay($Array, "Sorted Array")


Func _ArraySortNum(ByRef $n_array, $i_descending = 0, $i_start = 1)
    Local $i_ub = UBound($n_array)
    For $i_count = $i_start To $i_ub - 2
        Local $i_se = $i_count
        If $i_descending Then
            For $x_count = $i_count To $i_ub - 1
                If Number($n_array[$i_se]) < Number($n_array[$x_count]) Then $i_se = $x_count
            Next
        Else
            For $x_count = $i_count To $i_ub - 1
                If Number($n_array[$i_se]) > Number($n_array[$x_count]) Then $i_se = $x_count
            Next
        EndIf
        Local $i_hld = $n_array[$i_count]
        $n_array[$i_count] = $n_array[$i_se]
        $n_array[$i_se] = $i_hld
    Next
EndFunc   ;==>_ArraySortNum

8)

NEWHeader1.png

Link to comment
Share on other sites

Like This...

#include <array.au3>

$Array = StringSplit("2,5,3,4,6,1,8,9,7", ",")

_ArraySortNum($Array)

_ArrayDisplay($Array, "Sorted Array")


Func _ArraySortNum(ByRef $n_array, $i_descending = 0, $i_start = 1)
    Local $i_ub = UBound($n_array)
    For $i_count = $i_start To $i_ub - 2
        Local $i_se = $i_count
        If $i_descending Then
            For $x_count = $i_count To $i_ub - 1
                If Number($n_array[$i_se]) < Number($n_array[$x_count]) Then $i_se = $x_count
            Next
        Else
            For $x_count = $i_count To $i_ub - 1
                If Number($n_array[$i_se]) > Number($n_array[$x_count]) Then $i_se = $x_count
            Next
        EndIf
        Local $i_hld = $n_array[$i_count]
        $n_array[$i_count] = $n_array[$i_se]
        $n_array[$i_se] = $i_hld
    Next
EndFunc   ;==>_ArraySortNum

8)

Very thanks :)

Qui ose gagneWho Dares Win[left]CyberExploit[/left]

Link to comment
Share on other sites

  • 6 years later...

Hi.

Quite a time ago I had to search for all elements in one large for all the occurences in another, really *HUGE* array.

 

To Speed up the whole process, I sorted both Arrays, preserving positions in the 2nd arrays's element list (2D). By this I could skip the searching in the 2nd Array after looking up some few elements and increasing the "start value Position" in the 2nd Array to the next plausible one

Duplicates in both Arrays are possible.

Search-one-array-in-another.au3.

 

Regards, Rudi.

Edited by rudi

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

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