Jump to content

Array Sorting


Recommended Posts

I have a list of groups with contexts appended like so:

c_group.spo.cairns.qd

a_group.spo.cairns.qd

n_group.spo.cairns.qd

v1_group.inc.spo.cairns.qd

aa_group.inc.spo.cairns.qd

za_group.nda.mackay.qd

gb_group.nda.mackay.qd

aa_group.nda.mackay.qd

ab_group.inc.nda.mackay.qd

The group name is the first part before the first full stop eg. first one is c_group

The context is everything after the first full stop eg. first one is spo.cairns.qd

I need to sort the list so everything is grouped first alphabetically by context and then, within each of these groupings, alphabetically by the group name.

If I reverse everything then do a sort it sorts by context nicely, then reverse again to get list sorted by context. But then how to sort by group name inside that?

Any help greatly appreciated.

Link to comment
Share on other sites

You need a function that sorts on multiple columns. Search the Example Scripts forum and you will find something like

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

Thanks for the tip water. I ended up getting my act together and writing it myself.

#include 
#include 

$string = ClipGet()

$array = StringSplit($string,@CRLF,1)

;reverse contents of each element in array
for $x = 1 to $array[0]
  $array[$x] = _StringReverse($array[$x])
Next

;sort array with reversed contents to sort by context
_ArraySort($array)

;reverse elements back to original condition
for $x = 1 to $array[0]
  $array[$x] = _StringReverse($array[$x])
Next


$a = 1 ;starting element for sort of a context
$b = 0 ;ending point for a sort of a context

while $a < $array[0] ;step through all elements of array from 1
  $match = StringRegExp($array[$a],".*?\.(?i)(.*?QD)",1) ;context to match
  for $x = $a to $array[0]
    $thismatch = StringRegExp($array[$x],".*?\.(?i)(.*?QD)",1) ;context of this element
    if $thismatch[0] = $match[0] Then ;if contexts match
      $b = $x ;set end point of sort to this element
    endif
  Next
  _ArraySort($array,0,$a,$b) ;sort elements with the same context
  $a = $b + 1 ;set start element for next search
WEnd

_ArrayDisplay($array,"Two level sorted")

The regular exp<b></b>ressi&#111;n was a bit of a pain. The only way I could get it to work was to specify the last part of the context.

Any advice on the regex appreciated :)

Edited by gruntydatsun
Link to comment
Share on other sites

There are a number of examples of 2D arrays sort over multiple columns as a simple forum search shows.

It's a shame that we can use neither Assign nor Execute to build up a 2D array directly, else we could do that (doesn't work of course):

#include <Array.au3>

$string =    "c_group.spo.cairns.qd" & @CRLF & _
            "a_group.spo.cairns.qd" & @CRLF & _
            "n_group.spo.cairns.qd" & @CRLF & _
            "v1_group.inc.spo.cairns.qd" & @CRLF & _
            "aa_group.inc.spo.cairns.qd" & @CRLF & _
            "za_group.nda.mackay.qd" & @CRLF & _
            "gb_group.nda.mackay.qd" & @CRLF & _
            "aa_group.nda.mackay.qd" & @CRLF & _
            "ab_group.inc.nda.mackay.qd"
StringReplace($string, ".qd", "")
Local $count = @extended
Assign('array[' & $count & '][2]', StringTrimRight("[" & StringRegExpReplace($string & @CRLF, "(?is)(.*?)_group\.(.*?)(\R)", "[""$2"", ""$1""],"), 1) & "]", 1)
ConsoleWrite(@error & @TAB & VarGetType($array) & @LF)   ; no error reported but wrong type assigned
_ArrayDisplay($array)      ; nope!

Hence we need to do the work by ourselves:

#include <Array.au3>

;~ $string = ClipGet()
Local $string =    "c_group.spo.cairns.qd" & @CRLF & _
                "a_group.spo.cairns.qd" & @CRLF & _
                "n_group.spo.cairns.qd" & @CRLF & _
                "v1_group.inc.spo.cairns.qd" & @CRLF & _
                "aa_group.inc.spo.cairns.qd" & @CRLF & _
                "za_group.nda.mackay.qd" & @CRLF & _
                "gb_group.nda.mackay.qd" & @CRLF & _
                "aa_group.nda.mackay.qd" & @CRLF & _
                "ab_group.inc.nda.mackay.qd"
StringReplace($string, ".qd", "")
Local $count = @extended
Local $aValues = StringRegExp($string & @CRLF, "(?im)([^\.]+)\.(.*)\R", 3)
_ArrayDisplay($aValues)        ; for demo purpose

Local $array[$count][2]
For $i = 0 To $count - 1
    $array[$i][0] = $aValues[2 * $i + 1]    ; context
    $array[$i][1] = $aValues[2 * $i]        ; group name
Next
_ArrayDisplay($array)        ; for demo purpose

; now proceed to sort $array on column 1 then column 2 with some multi-column sort UDF

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Try this:

 

#include <Array.au3>

Local $string = "c_group.spo.cairns.qd" & @CRLF & _
                "a_group.spo.cairns.qd" & @CRLF & _
                "n_group.spo.cairns.qd" & @CRLF & _
                "v1_group.inc.spo.cairns.qd" & @CRLF & _
                "aa_group.inc.spo.cairns.qd" & @CRLF & _
                "za_group.nda.mackay.qd" & @CRLF & _
                "gb_group.nda.mackay.qd" & @CRLF & _
                "aa_group.nda.mackay.qd" & @CRLF & _
                "ab_group.inc.nda.mackay.qd"

$aTmp = StringSplit(StringStripCR($string), @LF, 2)
Global $array[UBound($aTmp)][2]
For $i = 0 To UBound($aTmp) - 1
    $array[$i][0] = StringRegExpReplace($aTmp[$i], "(?!<:.*)\..*", "$1")
    $array[$i][1] = StringRegExpReplace($aTmp[$i], "[^\.]+\.(.*)", "$1")
Next

Global $aI[2]
;order of sorting rows - here row 1, 0
$aI[0] = 1
$aI[1] = 0

_ArraySort_MultiColumn($array, $aI)
_ArrayDisplay($array)

; #FUNCTION# =============================================================================
; Name.............:    _ArraySort_MultiColumn
; Description ...:      sorts an array at given colums (multi colum sort)
; Syntax...........:    _ArraySort_MultiColumn(ByRef $aSort, ByRef $aIndices)
; Parameters ...:       $aSort - array to sort
;                       $aIndices - array with colum indices which should be sorted in specified order - zero based
;                       $oDir/$iDir - sort direction - if set to 1, sort descendingly else ascendingly
; Author .........:     UEZ
; Version ........:     v0.70 build 2013-11-20 Beta
; =========================================================================================
Func _ArraySort_MultiColumn(ByRef $aSort, ByRef $aIndices, $oDir = 0, $iDir = 0)
    If Not IsArray($aIndices) Or Not IsArray($aSort) Then Return SetError(1, 0, 0) ;checks if $aIndices is an array
    If UBound($aIndices) > UBound($aSort, 2) Then Return SetError(2, 0, 0) ;check if $aIndices array is greater the $aSort array
    Local $1st, $2nd, $x, $j, $k, $l = 0
    For $x = 0 To UBound($aIndices) - 1 ;check if array content makes sense
        If Not IsInt($aIndices[$x]) Then Return SetError(3, 0, 0) ;array content is not numeric
    Next
    If UBound($aIndices) = 1 Then Return _ArraySort($aSort, $oDir, 0, 0, $aIndices[0]) ;check if only one index is given
    _ArraySort($aSort, $oDir, 0, 0, $aIndices[0])
    Do
        $1st = $aIndices[$l]
        $2nd = $aIndices[$l + 1]
        $j = 0
        $k = 1
        While $k < UBound($aSort)
            If $aSort[$j][$1st] <> $aSort[$k][$1st] Then
                If $k - $j > 1  Then
                    _ArraySort($aSort, $iDir , $j, $k - 1, $2nd)
                    $j = $k
                Else
                    $j = $k
                EndIf
            EndIf
            $k += 1
        WEnd
        If $k - $j > 1 Then _ArraySort($aSort, $iDir, $j, $k, $2nd)
        $l += 1
    Until $l = UBound($aIndices) - 1
    Return 1
EndFunc

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

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