Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?

 

sort

 

Syntax

Release Information

Statement introduced in version 1.1 of the SLAX language, which is supported in Junos OS Release 12.2 and later releases.

Description

Control the order in which the for-each and apply-templates statements iterate through the current node list. By default, the for-each and apply-templates statements consider nodes in document order, but the sort statement defines the order prior to iterating through the node list. Insert the sort statement immediately after the for-each or apply-templates statement. The sort statement is only processed when the loop is first initiated.

The sort statement has an optional XPath expression and three optional parameters: case-order, data-type, and order. The XPath expression determines each node’s comparison string used for sorting. The script evaluates the expression with the node as its context, and then translates the result into the comparison string for that node. If you do not specify an XPath expression, the default value is ".", which causes the string content of each node in the list to be compared. SLAX-specific operators such as == and _ cannot be used within the expression string. If the sort statement does not include any optional parameters, the list is sorted based on the string value of each node.

The sort statement does not permanently sort the underlying XML data structure, only the order of the current node list being used by the for-each or apply-templates statement. Multiple sort statements can be assigned to a single for-each or apply-templates statement. They are applied, in order, until a difference is found.

Attributes

expressionXPath expression that determines each node’s comparison string used for sorting. The default value is ".".
case-orderSpecify whether to sort lowercase first or uppercase first. Acceptable values are “lower-first” or “upper-first”. The default is “upper-first”.
data-typeSpecify the element type, which determines whether a numerical, lexical, or other sort is performed. Acceptable values are “number” and “text”. The default is "text".

Setting data-type to “text” compares the strings based on their character values (that is ASCII code), so “0” is less than “9”, which is less than “A”, which is less than “Z”, which is less than “a”, which is less than “z”. Setting data-type to “number” converts the strings to numbers and compares them numerically. With ascending text sorting, “100” would come before “11” because “0” has a lower ASCII code than “1”, but with ascending number sorting, 11 would come before 100 because 11 is a smaller number than 100.

orderSpecify whether to sort in ascending or descending order. Acceptable values are “descending” or “ascending”. The default is “ascending”.

SLAX Example

The following example SLAX script executes the Junos XML API get-interface-information command and parses the resulting output. The for-each loop prints the name of each physical interface on the device sorted in ascending order.

Usage Examples

Related Documentation