Jump to content

Sort a 2D array on column with Numbers


Recommended Posts

Hello,
I try to automate ranking sort. Usually my classifications have votes at the end of line, eg:

Bat - 0
Bear - 3
Red Fox - 2
Fox - 1
Lion - 4
Cat - 1
Armadillo - 3
Monkey - 10

 my users want to order it and obtain

Monkey - 10
Lion - 4
Armadillo - 3
Bear - 3
Red Fox - 2
Cat - 1
Fox - 1
Bat - 0

You can notice, that list is double sorted :
- before numeric column
-  then rows with same vote (eg: cat -> fox)

I assemble some code :

#include <Array.au3>
#include<array.au3>
#include<file.au3>

Local $aArray_Base[0][2]

$file = FileOpen(@DesktopDir & '\animals.txt', 0)

While 1
   $line = FileReadLine($file)
   If @error = -1 Then ExitLoop

   $line_number = StringRegExp($line, '\d+', 1)

   if @error then
      $sFill = "" & "|" & $line
      _ArrayAdd($aArray_Base, $sFill)
   else
      $sFill = Number($line_number[0]) & "|" & $line
      _ArrayAdd($aArray_Base, $sFill)
   endif

WEnd
FileClose($file)

_ArrayDisplay($aArray_Base, "$avArray BEFORE _ArraySort()")
_ArraySort($aArray_Base, 0, 0, 0, 0)
_ArrayDisplay($aArray_Base, "$avArray AFTER _ArraySort() ascending column 0")
_ArraySort($aArray_Base, 0, 0, 0, 1)
_ArrayDisplay($aArray_Base, "$avArray AFTER _ArraySort() ascending column 1")
_ArraySort($aArray_Base, 0, 0, 0, 2)
_ArrayDisplay($aArray_Base, "$avArray AFTER _ArraySort() ascending column 2")

But I can't understand how use _arraysort...

Can you help me, please ?

thank you,
m.
 

Edited by myspacee
Link to comment
Share on other sites

Try this

#include <File.au3>
#include <Array.au3>

Opt ("MustDeclareVars", 1)

Local $array
_FileReadToArray ("animals.txt", $array, $FRTA_ENTIRESPLIT, " - ")
For $i = 0 to UBound ($array)-1
  $array[$i][1]=Number($array[$i][1])
Next
_ArraySort ($array, 0, 0, 0, 1)

Local $value = $array[0][1], $start = 0
For $i = 1 to UBound ($array)-1
  if $value = $array[$i][1] then ContinueLoop
  $value = $array[$i][1]
  if $i - $start > 1 then _ArraySort ($array, 0, $start, $i-1, 0)
  $start = $i
Next
if UBound ($array) - $start > 1 then _ArraySort ($array, 0, $start, Ubound($array)-1, 0)

_ArrayDisplay ($array)

 

Edited by Nine
Link to comment
Share on other sites

Adapt to my case :

#include <Array.au3>
#include<file.au3>

Local $Array[0][2]

$file = FileOpen(@DesktopDir & '\animals.txt', 0)

While 1
   $line = FileReadLine($file)
   If @error = -1 Then ExitLoop

   $line_number = StringRegExp($line, '\d+', 1)

   if @error then
      $sFill =  $line & "|" & ""
      _ArrayAdd($Array, $sFill)
   else
      $sFill = $line & "|" & Number(StringRegExpReplace($line_number[0], "[^0-9]", ""))
      _ArrayAdd($Array, $sFill)
   endif

WEnd
FileClose($file)


For $i = 0 to UBound ($array)-1
  $array[$i][1]=Number($array[$i][1])
Next
_ArraySort ($array, 1, 0, 0, 1)

Local $value = $array[0][1], $start = 0
For $i = 1 to UBound ($array)-1
  if $value = $array[$i][1] then ContinueLoop
  $value = $array[$i][1]
  if $i - $start > 1 then _ArraySort ($array, 0, $start, $i-1, 0)
  $start = $i
Next
if UBound ($array) - $start > 1 then _ArraySort ($array, 1, $start, Ubound($array)-1, 0)
_ArraySort ($array, 1, $start, Ubound($array)-1, 0)
_ArrayDisplay ($array)

Thank you!

m.

Link to comment
Share on other sites

hello, I need this script as part of onother one. Bigger and with its 'structure'.
My fault double sort at end. Thank you again, your solution is more simple than others i try.

m.
 

Edited by myspacee
Link to comment
Share on other sites

yall are wild

#include<array.au3>

local $a[8][2] = [['Bat' , 0],['Bear' , 3],['Red Fox' , 2],['Fox' , 1],['Lion' , 4],['Cat' , 1],['Armadillo' , 3],['Monkey' , 10]]
_ArraySort($a , 0 , 0 , 0 , 0 , 1)
_ArraySort($a , 1 , 0 , 0 , 1)
_ArrayDisplay($a)

 

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

Link to comment
Share on other sites

 

sort_working.thumb.PNG.671d7c6bdfa914f2985c5a349380307a.PNG

 

 

Edited by iamtheky
..i.,

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

Link to comment
Share on other sites

  • Moderators

myspacee,

You could also use my ArrayMultiColSort UDF (the link is in my sig):

#include<ArrayMultiColSort.au3>

Local $aList[8][2] = [['Bat' , 0],['Bear' , 3],['Red Fox' , 2],['Fox' , 1],['Lion' , 4],['Cat' , 1],['Armadillo' , 3],['Monkey' , 10]]

; Set sort order = First col 1 descending, second col 0 ascending
Local $aSortData[2][2] = [[1, 1], [0, 0]]

_ArrayMultiColSort($aList, $aSortData)

_ArrayDisplay($aList, "", 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

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