Important:
- This script has been updated on the 1/09/2014 to resolve the case where elements in the package have an empty alias.
- Features from this script have been enhanced and are now maintained in eaUtils Sparx add-in; please click here for more information.
I recently imported a number of requirements in my Enterprise Architect project with the following details: title, reference (stored in the requirement's alias), and description. By default Sparx Enterprise Architect sorts requirements within a given package by the element's name in the alphabetical order. When a package contains various types of elements (e.g. classes, interfaces, use cases, etc.), each group of elements from the same type is sorted separately. The following illustrates a mix of requirements, classes, and interfaces created within a package, in their default sorting order:
Going back to my requirements, I needed to sort them by the alias value e.g. REQ-TEST-001, REQ-TEST-002, REQ-TEST-003, etc. On the following screenshot, I illustrated on the left hand side the default sorting order in Enterprise Architect project browser, and on the right hand side the list of requirements in the desired alias order.
This article explains how to create and use an Enterprise Architect Project Browser script aimed at sorting elements from a selected package by the alias name. An additional script is provided at the end of the article to sort elements by a dedicated Tagged Value called "SortingOrder".
Enterprise Architect Project Browser scripts
Enterprise Architect lets you create project browser scripts. Once created in your project, these are available via a right click on a selected package from the browser, as illustrated below:
Sort by alias project browser script
To add the SortByAlias Project Browser script to your modelling project:
Step 1: open the scripting view using the Enterprise Architect menu Tools > Scripting.
Step 2: click on "New Project Browser Group" to store all user-defined scripts, e.g. Project Browser scripts.
Step 3: click on "New script > New VBScript" to define the "Sort By Alias" script, e.g. SortByAlias.
Step 4: open the script and copy/paste the following content (or download the VBScript text file here).
- option explicit
- !INC Local Scripts.EAConstants-VBScript
- '
- ' Script Name: SortbyAlias
- ' Author: Guillaume FINANCE, guillaume[at]umlchannel.com
- ' Purpose: Sort elements contained in the selected package from the Project Browser by the Alias name
- ' Date: 03/04/2014, updated on the 1/09/2014 (works when the package contains empty aliases, using Session.Output)
- '
- sub SortDictionary (objDict)
- ' constants
- Const dictKey = 1
- Const dictItem = 2
- ' variables
- Dim strDict()
- Dim objKey
- Dim strKey,strItem
- Dim X,Y,Z
- ' get the dictionary count
- Z = objDict.Count
- ' sorting needs more than one item
- If Z > 1 Then
- ' create an array to store dictionary information
- ReDim strDict(Z,2)
- X = 0
- ' populate the string array
- For Each objKey In objDict
- strDict(X,dictKey) = CStr(objKey)
- strDict(X,dictItem) = CStr(objDict(objKey))
- X = X + 1
- Next
- ' perform a a shell sort of the string array
- For X = 0 To (Z - 2)
- For Y = X To (Z - 1)
- If StrComp(strDict(X,dictItem),strDict(Y,dictItem),vbTextCompare) > 0 Then
- strKey = strDict(X,dictKey)
- strItem = strDict(X,dictItem)
- strDict(X,dictKey) = strDict(Y,dictKey)
- strDict(X,dictItem) = strDict(Y,dictItem)
- strDict(Y,dictKey) = strKey
- strDict(Y,dictItem) = strItem
- End If
- Next
- Next
- ' erase the contents of the dictionary object
- objDict.RemoveAll
- ' repopulate the dictionary with the sorted information
- For X = 0 To (Z - 1)
- objDict.Add strDict(X,dictKey), strDict(X,dictItem)
- Next
- ' sort the package elements based on the new sorting order
- dim newOrder
- newOrder = 0
- dim eaelement
- for each objKey in objDict
- Set eaelement = Repository.GetElementByGuid(objKey)
- 'change the position of the element in the package to the new sorting order value
- eaelement.TreePos = CLng(newOrder)
- eaelement.Update()
- newOrder = newOrder + 1
- next
- end if
- end sub
- sub sortElementsbyAlias (selectedPackage)
- Session.Output("Processing selected package " & selectedPackage.Name)
- dim elements as EA.Collection
- dim i
- dim processedElements
- set processedElements = CreateObject( "Scripting.Dictionary" )
- set elements = selectedPackage.Elements
- for i = 0 to elements.Count - 1
- dim currentElement as EA.Element
- set currentElement = elements.GetAt( i )
- Session.Output("Processing element " & i & " with type " & currentElement.Type & ", alias """ & currentElement.Alias & """ and guid " & currentElement.ElementGUID & ")")
- processedElements.Add currentElement.ElementGUID, currentElement.Alias
- next
- Session.Output("Sorting package elements")
- SortDictionary processedElements
- end sub
- '
- ' Project Browser Script main function
- '
- sub OnProjectBrowserScript()
- Repository.ClearOutput "Script"
- Session.Output( "Starting SortbyAlias script" )
- Session.Output( "==============================" )
- ' Get the type of element selected in the Project Browser
- dim treeSelectedType
- treeSelectedType = Repository.GetTreeSelectedItemType()
- select case treeSelectedType
- case otPackage
- ' Code for when a package is selected
- dim thePackage as EA.Package
- set thePackage = Repository.GetTreeSelectedObject()
- sortElementsbyAlias thePackage
- Repository.RefreshModelView (thePackage.PackageID)
- case else
- ' Error message
- Session.Prompt "This script does not support items of this type.", promptOK
- end select
- end sub
- OnProjectBrowserScript
Step 5: save the script (Ctrl-S).
Step 6: right click on the target package from the Project Browser where elements need to be sorted by the alias, and select Scripts > SortbyAlias
Result: the requirements have been sorted by the alias value as illustrated below.
This sorting order is consistent with the requirements' alias values:
Sort by tagged value 'Sorting Order' project browser script
In some cases, the alias value may not be appropriate to sort your elements within the selected package. Based on the above SortByAlias script, I created the SortbyTaggedValue_SortingOrder script aimed at sorting elements using a tagged valued created for this purpose, SortingOrder.
To illustrate its use, I added the SortingOrder tagged value to each requirement:
To add the SortByTaggedValue Project Browser script to your project:
Step 1: open the scripting view using the Enterprise Architect menu Tools > Scripting.
Step 2: open the Project Browser scripts group.
Step 3: click on "New script > New VBScript" to define the "Sort By Tagged Value" script, e.g. SortbyTaggedValue_SortingOrder.
Step 4: open the script and copy/paste the following content from the script that can be downloaded here.
Step 5: save the script (Ctrl-S).
Step 6: right click on the target package from the Project Browser where elements need to be sorted by the alias, and select Scripts > SortbyTaggedValue_SortingOrder.
Result: the requirements have been sorted by the SortingOrder tagged value as illustrated below.
This article illustrated how Sparx Enterprise Architect can be tailored to introduce new features via user defined scripts, e.g. to apply a new sorting rule on the elements from a selected package. It could be further improved e.g. to sort elements within the sub packages and so on.