Jump to content

How to sort the 4th or 5th column of an array.


tommytx
 Share

Go to solution Solved by Bowmore,

Recommended Posts

this is in a text file out-short.txt
0001|John|10|dog|017|tan|
0002|Susy|29|cat|023|man|
0003|Trac|30|hat|055|dan|
0004|Brac|12|fat|094|pan|

I have the above data in a text file.. and want to be able to load and sort by any of the columns...

Local $aRec
_FileReadToArray("out-short.txt", $aRec)
_ArrayDisplay($aRec)

$avArray = StringSplit($aRec, "|")

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

None of the above is working for me... any suggestions..

Does this apply only to a 2 dimensional array and I have a 5 dimensional array..

For example I want to be able to say sort in ascending column 4 etc...

Link to comment
Share on other sites

for $i = 1 to $aRec[0]
$dog = StringSplit($aRec[$i], "|")
_ArrayDisplay($dog)
$avArray[$i][1] = $dog[1]
$avArray[$i][2] = $dog[2]
$avArray[$i][3] = $dog[3]
$avArray[$i][4] = $dog[4]
$avArray[$i][5] = $dog[5]
$avArray[$i][6] = $dog[6]
Next

Well thanks but telling me I already have a 2d did not help...

Do i need to run it thru something like this first before using the sort array... I am lost... thanks

I did try the above but did not seem to work either...

Edited by tommytx
Link to comment
Share on other sites

  • Solution

You were nearly there. This will give you the expected result.

#include <file.au3>
#include <array.au3>
Local $avArray
_FileReadToArray("out-short.txt", $avArray, $FRTA_NOCOUNT, "|")
_ArrayDisplay($avArray, "$avArray BEFORE _ArraySort()")
_ArraySort($avArray, 0, 0, 0, 0)
_ArrayDisplay($avArray, "$avArray AFTER _ArraySort() ascending column 0")
_ArraySort($avArray, 0, 0, 0, 1)
_ArrayDisplay($avArray, "$avArray AFTER _ArraySort() ascending column 1")
_ArraySort($avArray, 0, 0, 0, 2)
_ArrayDisplay($avArray, "$avArray AFTER _ArraySort() ascending column 2")

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

Link to comment
Share on other sites

Thanks  i feel like I am making progress now...

First gave me the below so I declared the variable and all is well on that one.

"Variable used w/o being declared... so I declared it and that was fine.  , $FRTA_NOCOUNT

Now its giving me the below problem. Do I maybe just need to add another comma to fake another parameter that is not needed?

"C:\Important\top-dollar.au3" (12) : ==> Incorrect number of parameters in function call.:
_FileReadToArray("out-short.txt", $avArray, $FRTA_NOCOUNT, "|")
^ ERROR
>Exit code: 1 Time: 0.353

Any suggestions?
And thanks for that.. I had no idea I could load it and string split it in one flail swoop... pretty neat.

However when I delete the , $FRTA_NOCOUNT it will then work but the only column that sorts is the first one and of course the count number is in the sort but as you punch thru none of the other columns sort at all... when the first column sorts all the others follow suit with the first column.. which is good.. but as you continue no further sorting occurs..
Also the inclusion of the "|" in the _filereadtoarray command is not causing a string split.. as I have never seen that used before and could not find reference that it will work... is that something new... bypassing stringsplit by adding it there?

IF I cannot get this to work will have to use the old fashioned cocantenate the desired sort column to the front then sort and drop the first column.. but that is like going around your elbow to get to your thumb...

Edited by tommytx
Link to comment
Share on other sites

Thank you John.. you were exactly right.... I had the verision 3.3.10 and currently its 3.3.12

there was a clue that I was out of date.. with my old versioin it told me I needed to declare $FRTA_NOCOUNT,

and when I upgraded it complained that you cannot double delcare a constant... so obviously the $FRTA thingy has just been added..

anyway removed it from declare and all worked well...  and in fact the automatic string splitter now works... that will be a useful tool for

me being able to string split while loading the array.... great addition.... this sucker keeps getting smarter and smarter..

Would love to give you  credit... for this but Bowmore actually solved my problem.. but little did I realize I was running an old version till you came along... too bad there is not two checkboxes for solutions... but thank you so very much..... for your help..

Edited by tommytx
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...