Jump to content

Recommended Posts

Posted

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.

Posted

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 2024-07-28 - Version 1.6.3.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 (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

 

Posted (edited)

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
Posted

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)

Posted (edited)

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 a 2D array at given columns (multi column sort)
; Syntax...........:    _ArraySort_MultiColumn(ByRef $aSort, ByRef $aIndices)
; Parameters ...:       $aSort - array to sort
;                       $aIndices - array with column indices which should be sorted in specified order - zero based
;                       $oDir/$iDir - sort direction - if set to 1, sort descending else ascending. $oDir is for the 1st column,
;                                                     $iDir for the remaining columns
;                       $iStart - where to start to sort - 0 = 0 based array, 1 = 1 based array
; Return values.:       Success: 1
;                       Failure: 0 and sets @error:
;                       1 - $aSort or $aIndices is not an array
;                       2 - $aIndices has more entries than $aSort has columns
;                       3 - $aIndices contains a non-integer value
;                       4 - $aSort is not a 2D array
;                       5 - $aIndices contains an out-of-range column index
;                       6 - $aIndices contains duplicate column indices
;                       7 - internal _ArraySort call failed (@extended holds its @error)
; Author .........:     UEZ
; Version ........:     v0.90 build 2026-07-18 Beta
; =========================================================================================
Func _ArraySort_MultiColumn(ByRef $aSort, ByRef $aIndices, $oDir = 0, $iDir = 0, $iStart = 0)
    If Not IsArray($aIndices) Or Not IsArray($aSort) Then Return SetError(1, 0, 0) ;checks if $aSort / $aIndices are arrays
    Local $iCols = UBound($aSort, 2)
    If $iCols = 0 Then Return SetError(4, 0, 0) ;array is 1D only
    Local $iKeys = UBound($aIndices)
    If $iKeys > $iCols Then Return SetError(2, 0, 0) ;check if $aIndices array is greater than $aSort column count
    Local $x, $m, $j, $k, $l, $bNewGroup
    Local $aIdx[$iKeys] ;local integer copy - caller's $aIndices stays untouched
    For $x = 0 To $iKeys - 1 ;validate array content
        If IsNumber($aIndices[$x]) Then
            If $aIndices[$x] <> Int($aIndices[$x]) Then Return SetError(3, 0, 0) ;fractional number
        ElseIf Not StringRegExp($aIndices[$x] & "", "^[-+]?\d+$") Then
            Return SetError(3, 0, 0) ;not an integer value
        EndIf
        $aIdx[$x] = Int($aIndices[$x])
        If $aIdx[$x] < 0 Or $aIdx[$x] > $iCols - 1 Then Return SetError(5, 0, 0) ;column index out of range
        For $m = 0 To $x - 1
            If $aIdx[$m] = $aIdx[$x] Then Return SetError(6, 0, 0) ;duplicate column index
        Next
    Next
    $iStart = Int($iStart) < 0 ? 0 : Int($iStart) > 1 ? 1 : Int($iStart)
    _ArraySort($aSort, $oDir, $iStart, 0, $aIdx[0]) ;initial sort by first key column
    If @error Then Return SetError(7, @error, 0)
    If $iKeys = 1 Then Return 1 ;only one index given -> done
    For $l = 0 To $iKeys - 2 ;one pass per additional key column
        $j = $iStart
        $k = $iStart + 1
        While $k < UBound($aSort)
            $bNewGroup = False
            For $m = 0 To $l ;compare ALL previous key columns, not just the current one
                If $aSort[$j][$aIdx[$m]] <> $aSort[$k][$aIdx[$m]] Then
                    $bNewGroup = True
                    ExitLoop
                EndIf
            Next
            If $bNewGroup Then
                If $k - $j > 1 Then
                    _ArraySort($aSort, $iDir, $j, $k - 1, $aIdx[$l + 1])
                    If @error Then Return SetError(7, @error, 0)
                EndIf
                $j = $k
            EndIf
            $k += 1
        WEnd
        If $k - $j > 1 Then ;last group at the end of the array
            _ArraySort($aSort, $iDir, $j, $k - 1, $aIdx[$l + 1])
            If @error Then Return SetError(7, @error, 0)
        EndIf
    Next
    Return 1
EndFunc   ;==>_ArraySort_MultiColumn

Br,

UEZ

Edited by UEZ
Code update

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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

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
×
×
  • Create New...