Jump to content

..


Recommended Posts

  • Developers

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

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
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
;
Link to comment
Share on other sites

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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

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