Jump to content

arrays sorting (arranging) by last two characters


Recommended Posts

Hi coders,
I'm having trouble with arrays. I decided to stay at 1D array because I think it's simpler but I got a problem as seen here:
the array contains elements like this: "thingX-XX
 

Local $array = ["thing1-08", "thing2-09", "thing3-07", "thing4-05", "thing5-10"]

The result should be this:
 

Local $array = ["thing4-05", "thing3-07",..., "thing5-10"]

Any ideas how to arrange that like this? Thank you in advance.

Link to comment
Share on other sites

  • Moderators

knucklesCZ,

This is one way of doing it: :)

#include <Array.au3>

Local $array = ["thing1-08", "thing2-09", "thing3-07", "thing4-05", "thing5-10"]

; Add a new column
_ArrayColInsert($array, 1)

For $i = 0 To UBound($array) - 1
    ; Extract numeric part and add to new column
    $array[$i][1] = StringRegExpReplace($array[$i][0], ".*-(.*)", "$1")

Next

_ArrayDisplay($array, "Col added", Default, 8)

; Sort on the number column
_ArraySort($array, Default, Default, Default, 1)

_ArrayDisplay($array, "Sorted", Default, 8)

; Delete the column
_ArrayColDelete($array, 1)

_ArrayDisplay($array, "Col deleted", Default, 8)
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

Well actually after sorting, the array remains a 2D array (with 2 cols)
 

I think that can be also seen in the _ArrayDisplay table where it says the array is

$array[5][1]

in the final step where the second col is deleted. Thus the array should be 1D (1 col array) again.

but I can't use it as 1D -->

Array variable has incorrect number of subscripts or subscript dimension range exceeded.:
MsgBox($MB_SYSTEMMODAL, "", $array[0])
MsgBox($MB_SYSTEMMODAL, "", ^ ERROR

Instead, 2D things are working
 

MsgBox($MB_SYSTEMMODAL, "", $array[0][0])   <--- that goes fine

That's strange.


+ only this works (@down)

MsgBox($MB_SYSTEMMODAL, "", $array[0][0])
MsgBox($MB_SYSTEMMODAL, "", $array[1][0])
MsgBox($MB_SYSTEMMODAL, "", $array[2][0])

this doesn't though (@down)

MsgBox($MB_SYSTEMMODAL, "", $array[0][0])
MsgBox($MB_SYSTEMMODAL, "", $array[1][1])
MsgBox($MB_SYSTEMMODAL, "", $array[2][2])
Edited by knucklesCZ
Link to comment
Share on other sites

Convert 2D arrays to 1D array

 

#include <Array.au3>

Local $array = ["thing1-08", "thing2-09", "thing3-07", "thing4-05", "thing5-10"]
_ArrayDisplay($array, "No Sort", Default, 8)
_ArrayColInsert($array, 1)
For $i = 0 To UBound($array) - 1
    $array[$i][1] = StringRegExpReplace($array[$i][0], ".*-(.*)", "$1")
Next
_ArraySort($array, Default, Default, Default, 1)
_ArrayColDelete($array, 1)
Local $a,$NewArray[UBound($array)]
For $i = 0 To UBound($array) - 1
    $NewArray[$i] =$array[$i][0]
Next
$array=$NewArray
_ArrayDisplay($array, "Sorted", Default, 8)
MsgBox(0, "", $array[0])

Regards,
 

Link to comment
Share on other sites

#include <Array.au3>

Local $array = ["thing1-08", "thing2-09", "thing3-07", "thing4-05", "thing5-10"]

_MixArray($array)
_ArraySort($array)
_MixArray($array)
_ArrayDisplay($array)
MsgBox(0, "", $array[0])

Func _MixArray(ByRef $array)
   For $i = 0 To UBound($array) - 1
       $array[$i] = StringRegExpReplace($array[$i], "(.*)(-)(.*)", "$3$2$1")
   Next
EndFunc

But you are right, this array trouble is strange indeed

Link to comment
Share on other sites

  • Moderators

knucklesCZ,

My fault. As explained in the Help file, you need to use the $bConvert parameter in _ArrayColDelete to force the array back to 1D when there is only a single column:

; Delete the column
_ArrayColDelete($array, 1, True)
And to think that I wrote the function too! :blush:

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

i dont have time to test if this lucky for this particular arrangement, but

#include <Array.au3>

Local $array = ["thing1-08", "thing2-09", "thing3-07", "thing4-05", "thing5-10"]


for $i = 1 to ubound($array) - 1

If number(StringRegExp($array[$i] , "\-(\d+)\z" , 3)[0]) > number(StringRegExp($array[$i - 1] , "\-(\d+)\z" , 3)[0]) Then
    _arrayswap($array , $i , $i - 1)
    $i = 0
EndIf

next

_ArrayDisplay($array)
Edited by boththose

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

:tv_happy:

i dont have time to test if this lucky for this particular arrangement, but

#include <Array.au3>

Local $array = ["thing1-08", "thing2-09", "thing3-07", "thing4-05", "thing5-10"]


for $i = 1 to ubound($array) - 1

If number(StringRegExp($array[$i] , "\-(\d+)\z" , 3)[0]) > number(StringRegExp($array[$i - 1] , "\-(\d+)\z" , 3)[0]) Then
    _arrayswap($array , $i , $i - 1)
    $i = 0
EndIf

next

_ArrayDisplay($array)

descending

Edited by Trong

Regards,
 

Link to comment
Share on other sites

#include <Array.au3>

Local $array = ["thing1-08", "thing2-09", "thing3-07", "thing4-05", "thing5-10"]


for $i = 1 to ubound($array) - 1

If number(StringRegExp($array[$i] , "\-(\d+)\z" , 3)[0]) < number(StringRegExp($array[$i - 1] , "\-(\d+)\z" , 3)[0]) Then
    _arrayswap($array , $i , $i - 1)
    $i = 0
EndIf

next

_ArrayDisplay($array)

 

just flip the sign, my bad.   and it seems to hold up in prelim testing

Edited by boththose

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

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