Sign in to follow this  
Followers 0
ArxBzFhblxphS

..

5 posts in this topic

#1 ·  Posted (edited)

Posted Image Edited by Thanubis

Share this post


Link to post
Share on other sites



Is it possible to sort a 2d array by multiple columns at once?

I want to sort an array by column 0, then by column 1, and then by column 2. This all has to be done in a single sort because multiple sorts overwrite any previous sorts.

Is there an function that can do this?

Add an extra column in your array and concatenate the 3 columns in it after which you can sort it. Just remember that Numeric data needs to be preceded with zeros and made the same length.

Jos


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Here is the reverse of the frequently used "string - array" approach, being:-

Starting with an string.

Convert the string into a array to be able to work on the string in its array form.

Then, convert the array back into an string.

;
#include <Array.au3>

Local $avArray[5][3] = [ _
        [5, 20, 8], _
        [4, 32, 7], _
        [3, 16, 9], _
        [2, 35, 0], _
        [1, 19, 6]]

Local $sStr = "", $sSorted

$iRow = UBound($avArray)
$iCol = UBound($avArray, 2)

;Array to string
For $r = 0 To $iRow - 1
    For $c = 0 To $iCol - 1
        $sStr &= $avArray[$r][$c] & @CRLF
    Next
Next

$sSorted = _StringSortRE($sStr, 0, 1)

;String to array
For $c = 0 To $iCol - 1
    For $r = 0 To $iRow - 1
        $avArray[$r][$c] = StringRegExpReplace($sSorted, "^(?:.*\v+?|$){" & ($c * $iRow) + $r & "}(.*\v+|$)(?s).*", "\1") ; Return line no. 0-based
    Next
Next

_ArrayDisplay($avArray, "Sorted Array")

;For descending order set $iDescending to a numeric value not zero.
;To sort numbers set $iNumbers to a numeric value not zero.
Func _StringSortRE($sStr, $iDescending = 0, $iNumbers = 0)
    Local $iCount, $bPass = False, $x = 1
    StringReplace($sStr, @CRLF, @CRLF)
    $iCount = @extended
    Local $sStr1 = $sStr

    While Not $bPass
        $bPass = True
        For $x = 0 To $iCount - 2
            $sline1 = StringRegExpReplace($sStr1, "^(?:.*\v+?|$){" & $x & "}(.*?)(?:\v+|$)(?s).*", "\1")
            $sline2 = StringRegExpReplace($sStr1, "^(?:.*\v+?|$){" & $x + 1 & "}(.*?)(?:\v+|$)(?s).*", "\1")
            If ($iNumbers <> 0 And $iDescending = 0 And Number($sline1) > Number($sline2)) Or _
                    ($iNumbers = 0 And $iDescending = 0 And $sline1 > $sline2) Or _
                    ($iNumbers <> 0 And $iDescending <> 0 And Number($sline1) < Number($sline2)) Or _
                    ($iNumbers = 0 And $iDescending <> 0 And $sline1 < $sline2) Then
                $sStr1 = StringRegExpReplace($sStr1, "(?m)((^.*\v+|\v+){" & $x & "})(.*\v+|\v+)(?s)(.*)", "${1}" & $sline2 & @CRLF & "\4") ; Return string with replaced line. (0-based. line) line
                $sStr1 = StringRegExpReplace($sStr1, "(?m)((^.*\v+|\v+){" & ($x + 1) & "})(.*\v+|\v+)(?s)(.*)", "${1}" & $sline1 & @CRLF & "\4") ; Return string with replaced line. (0-based. line) line
                $bPass = False
            EndIf
        Next
    WEnd
    Return $sStr1
EndFunc   ;==>_StringSortRE
;

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Please have a look at this post. The code sorts an 2d array with up to 3 columns.

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

This is one I wrote some time ago

Sort 2D array on multiple columns


"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

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