Sign in to follow this  
Followers 0
Mechaflash

Sorting 2D arrays by values of two columns

7 posts in this topic

Has someone created a snippet or UDF to Sort a 2D array by values of two columns?

For example, array looks like this.

[10, 10]

[20, 10]

[10, 0]

[20, 0]

I want to sort by column one AND THEN by column two so it looks like

[10, 0]

[10, 10]

[20, 0]

[20, 10]

Using the _ArraySort() only allows you to sort by a single column, so the data would come out like

[10, 10]

[10, 0]

[20, 10]

[20, 0]

If no one has a UDF/snippet already made, I'll create one, but just wanted to make sure I'm not wasting my time if it's already out there.


Spoiler

“Hello, ladies, look at your man, now back to me, now back at your man, now back to me. Sadly, he isn’t me, but if he stopped using ladies scented body wash and switched to Old Spice, he could smell like he’s me. Look down, back up, where are you? You’re on a boat with the man your man could smell like. What’s in your hand, back at me. I have it, it’s an oyster with two tickets to that thing you love. Look again, the tickets are now diamonds. Anything is possible when your man smells like Old Spice and not a lady. I’m on a horse.”

 

Share this post


Link to post
Share on other sites



I haven't tried it. but you probably could sort the first column. Then examine the array to find the start and end of each series in the first column assign it to start and end. specify the second column with its start and end.

#Include <Array.au3>

_ArraySort(ByRef $avArray[, $iDescending = 0 [, $iStart = 0 [, $iEnd = 0 [, $iSubItem = 0]]]])

Share this post


Link to post
Share on other sites

<------------ Dummy

I haven't tried it. but you probably could sort the first column. Then examine the array to find the start and end of each series in the first column assign it to start and end. specify the second column with its start and end.

I didn't even think about that... XD


Spoiler

“Hello, ladies, look at your man, now back to me, now back at your man, now back to me. Sadly, he isn’t me, but if he stopped using ladies scented body wash and switched to Old Spice, he could smell like he’s me. Look down, back up, where are you? You’re on a boat with the man your man could smell like. What’s in your hand, back at me. I have it, it’s an oyster with two tickets to that thing you love. Look again, the tickets are now diamonds. Anything is possible when your man smells like Old Spice and not a lady. I’m on a horse.”

 

Share this post


Link to post
Share on other sites

Everyone's third eye gets tunnel vision occasional. If it needs to be tight I would modify the _array to dump the start/ends into a second array, so you don't have to go through the array more than necessary.

Share this post


Link to post
Share on other sites

You could try this one:

Share this post


Link to post
Share on other sites

Or this...

Global $sorted = 0, $tmp
_ArraySort($aArray, 0, 1, 0, 0) ; sort on column 0
While $sorted = 0 ; sub-sort on column 1
    $sorted = 1
    For $x = 1 to $aArray[0][0] -1
        If  $aArray[$x][0] = $aArray[$x + 1][0] _
        And $aArray[$x][1] > $aArray[$x + 1][1] Then
           $tmp = $aArray[$x + 1][1]
           $aArray[$x + 1][1] = $aArray[$x][1]
           $aArray[$x][1] = $tmp
           $sorted = 0
       EndIf
    Next
WEnd

you'd need to make a minor mod or two for a 0-based array and flip the ">" to a "<" were you sorting descending

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  
Followers 0