Jump to content
Sign in to follow this  
SiteMaze

Multi key sort

Recommended Posts

SiteMaze

Hello,

How to do a multikey sort of a 2d array?

I want to sort a CSV file, so I read the file to a 2d array. Then I want to do a mutikey sort, first sort by column A, then by column B.

I have search the forum and all it came up is one level sort.

Thanks in advance.

Share this post


Link to post
Share on other sites
weaponx

This didn't work for you?

#include <Array.au3>

Dim $array[4][2]

$array[0][0] = "A"
$array[0][1] = "Z"

$array[1][0] = "B"
$array[1][1] = "A"

$array[2][0] = "C"
$array[2][1] = "A"

$array[3][0] = "A"
$array[3][1] = "B"

;_ArrayDisplay($array)

_ArraySort($array, 0,0,0,2,0)

_ArrayDisplay($array)
Edited by weaponx

Share this post


Link to post
Share on other sites
randallc

Hi,

(btw @ WeaponX, that sort only sort sequentialy on one column at a time, I think, not on subsorts for matching rows?)

See the link in my signature for Attay2D; I think the zip file wih examples is still there too.

PM me if an problems; it is aalso udes to show it is woking in the script SearchMIner in the display; it has to be converted to ArrayOfStrings by the fnction first; I had it in "Au3" pure if you can't use vbs for any reason, but it was slow on large sorts.

best Randall

$i_ColSortIndex = "2|-5|3";,[numbered by base 1 (ie first col number is "1")] [Sorts on cols 2,4,3 {DESC on 4 as NEGATIVE}]
;===============================================================================
$timerstamp3 = TimerInit()
_Array2D1DFieldSortSt($ar_TempSingle, $i_ColSortIndex);_Array2D1DFieldSortSt

[or serach forum on "subsort"]

Edited by randallc

Share this post


Link to post
Share on other sites
weaponx

The example I posted works as expected.

Share this post


Link to post
Share on other sites
randallc

Hi,

Not sure what you are saying?

Do you really believe simple "ArraySort" has such a capability except by coincidence?

How yould yo get this to sort on cols 1, then with subsort on 3?

; SubsotrtWeaponX.au3
#include <Array.au3>

Dim $array[8][4]=[["C","A","A","C"],["A","A","A","D"],["D","A","D","A"],["A","A","C","B"],["C","A","A","C"],["A","A","A","D"],["D","A","D","A"],["A","A","C","B"]]

_ArrayDisplay($array)

_ArraySort($array, 0,0,0,UBound($array,2),1)

_ArrayDisplay($array)
_ArraySort($array, 0,0,0,UBound($array,2),3)

_ArrayDisplay($array)

Share this post


Link to post
Share on other sites
weaponx

All I said was that the example works. Maybe it is too simple.

Share this post


Link to post
Share on other sites
SiteMaze

I haven't yet tried out the suggestions yet, but will try out soon.

I have another question: Can I achived multikey sort by merging the respective columns?

for example,

1. Merge 2d array into a 1d array [1 2 3 3, A B D C] > [1A 2B 3D 3C]

2. _ArraySort on 1d array

What are the drawbacks?

Share this post


Link to post
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
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.