Sorting
sort()
enables you to sort a set of values or nodes based on their value, either numerical, alphabetical or date-based
Sorting values
The simplest possible sorting method is to add sort()
to a list of values that you wish to order
By default, it sorts numbers in ascending order and then sorts strings ascending alphabetically
array(2,1,'b','c','a').sort()
returns 1,2,a,b,c
To override this default behaviour, use a slightly different syntax: sort('Property', 'direction')
array(2,1,'b','c','a').sort('value', 'desc')
This returns c, b, a, 2, 1
Sorting strings
The same syntax can be used to sort by the values of a dimension in Orgvue sort('Property', 'direction')
node.c.sort('grade', 'asc')
This will return a list of the node's children, sorted in alphabetical order of their grade (A-Z)
Sorting numerical data
It is possible to apply the above syntax when sorting a measure, however, it may not return the expected order because the .sort('Property', 'direction')
syntax sorts the measure as if it were a string
e.g. 9.9 -> 800 -> 70 -> 6060 .....
This is shown in the example below where the values have been sorted in descending order but the node with the salary "52.2K" is sorted higher than the node with salary of 190k
To correctly sort numerical fields, use the syntax:
.sort('Property', sort.number | sort.direction)
Note the use of
|
to separate thesort.number
andsort.direction
in the expression
node.c.sort('current_salary', sort.number|sort.desc)
This will return a list of the node's children, correctly sorted in descending order of their Current Salary (high-low)
If you miss out the |sort.direction
argument in numerical sorts, a sort.asc
will be applied by default
You can combine criteria to sort in a more nuanced way
To do this, chain the sort criteria together inside sort()
, for example:
node.ad(0).sort('department', 'asc','current_salary', sort.number|sort.desc)
Which sorts all the descendants of a node and the node itself first by Department alphabetically, then by Current Salary in descending order