Sorting Lists and Sublists

The ProcScript commands sort/list, $sortlist, and $sortlistid enable you to sort all kinds of lists, including indexed lists (flat lists), associative lists (such as property lists and ValRep lists), and nested lists (such as those used in tree widgets).

When sorting lists you can specify an element to use as the basis of the sort, as well as sort options. ( For more information, see Using Sort Options.)

  • $idpart—sort on the IdPart of an associative list
  • $valuepart—sort on the ValuePart of an associative list
  • $string(SublistItemId)—sort on the value part (as a string) of the sublist item with ID SublistItemId of an associative sublist
  • SublistItemNr—sort on the sequence number of an indexed sublist

Lists with sublists are analogous to occurrences of entities, and the techniques for sorting them are essentially the same. However there are several points on which they differ:

  • Instead of using sort/e, you use sort/list, $sortlist, or $sortlistid. ($sortlistid is only required when sorting an associated list with sublists.)
  • The data type of list items is not known, so all lists are sorted as String, whereas entity fields have a specific data type, which is taken into consideration when sorting on non-String fields.
  • When sorting lists and sublists, you can sort using a sublist item by specifying its position number within the indexed list.

The following examples demonstrate how to use different elements to sort on. In most cases, they use $sortlist, but they apply equally to sort/list and $sortlistid.

Sorting Lists by ID or Value

You can choose to sort by the IdPart or the ValuePart of the item.

Sorting a ValRep

ProcScript

Result

Define the ValRep list of the COLORS field:

$valrep(COLORS) = "1=Red;3=Green;2=Blue"

Graphical content.

Sort by the value (in a ValRep, this is the Representation):

vColors = $valrep(COLORS)
vSortedList = $sortlist (vColors)
$valrep(COLORS) = vSortedList

Graphical content.

Sort by the ID (in a ValRep, this is the Value):

vColors = $valrep(COLORS)
vSortedList = $sortlist (vColors, "$idpart")
$valrep(COLORS) = vSortedList

Graphical content.

Note:  A field can only have one current value, so the COLORS ValRep list must be copied to a variable before it is sorted. For more information, see ValRep Lists and Ranges .

Sorting Nested Lists

You can sort nested lists using the item ID or the item value of sublist items. The examples that follow use the illustrated lists to demonstrate how the sort parameters work. (Spaces and line breaks have been added for readability).

List1: Index List of Associative Sublists

List1 = 
4=Los Angeles !; 3=Sacramento   !; 1=San Francisco ;
1=Victoria    !; 2=Vancouver    !; 3=Prince George ;
3=Munchen     !; 1=Berlin       !; 2=Hamburg

List2: Associative List of Associative Sublists

List2 =
CA=4=Los Angeles !; 3=Sacramento   !; 1=San Francisco;
BC=1=Victoria    !; 2=Vancouver    !; 3=Prince George;
DE=3=Munchen     !; 1=Berlin       !; 2=Hamburg

Sorting Sublists by Sublist Item Number

To sort the lists using the second item of each sublist, use command:

$sortlist(List, "2")

For example

vSortedList1 = $sortlist(LIST1, "2:A") 
vSortedList2 = $sortlist(LIST2, "2:A")   

The lists are sorted in ascending order of the second item in the sublist. Because the first character of the second is a number, the lists are sorted in the order of: 1=Berlin, 2=Vancouver, 3=Sacramento.

Result
List1 (Index List or Sublists)
3=Munchen      !; 1=Berlin     !; 2=Hamburg       ;
1=Victoria     !; 2=Vancouver  !; 3=Prince George ;
4=Los Angeles  !; 3=Sacramento !; 1=San Francisco
List2 (Associative List of Sublists)
DE=3=Munchen     !; 1=Berlin       !; 2=Hamburg       ;
BC=1=Victoria    !; 2=Vancouver    !; 3=Prince George ;
CA=4=Los Angeles !; 3=Sacramento   !; 1=San Francisco

Sorting Sublists by Sublist Item ID

To sort the list by the value part (as a string) of a specific sublist item, specify the literal sublist item ID using the $string(SublistItemID) argument:

$sortlist(List, "$string(2)")

For example:

vSortedList1 = $sortlist(LIST1, "$string(2):a")
vSortedList2 = $sortlist(LIST2, "$string(2):a")

Note:  If a sublist does not contain items with the specified ID, it is placed first in the resulting sort. In this case, the the lists are sorted in the order no result, 2=Hamburg, 2=Vancouver.

Result
List1 (Index List or Sublists)
4=Los Angeles !; 3=Sacramento !; 1=San Francisco ;
3=Munchen     !; 1=Berlin     !; 2=Hamburg       ;
1=Victoria    !; 2=Vancouver  !; 3=Prince George 
List2 (Associative List of Sublists)
CA=4=Los Angeles !; 3=Sacramento !; 1=San Francisco ;
DE=3=Munchen     !; 1=Berlin     !; 2=Hamburg       ;
BC=1=Victoria    !; 2=Vancouver  !; 3=Prince George

Sorting Sublists by List Item ID

To sort the list by the ID of the outer list, use the $idpart argument:

$sortlist(List, "$idpart")

For example:

vSortedList1 = $sortlist(LIST1, "$idpart:a") 
vSortedList2 = $sortlist(LIST2, "$idpart:a")

The results of the sort in the two lists are now different because Uniface treats both lists as associative lists. However, vList1 is an indexed list, so the ID of the first sublist item becomes the ID of the entire sublist.

Thus, the indexed list is sorted as 1=victoria …, 3=Munchen …, 4=Los Angeles …. The associative list is sorted as BC=, CA=, DE=.

Result
List1 (Index List or Sublists)
1=Victoria    !; 2=Vancouver   !; 3=Prince George;
3=Munchen     !; 1=Berlin      !; 2=Hamburg;
4=Los Angeles !; 3=Sacramento  !; 1=San Francisco
List2 (Associative List of Sublists)
BC=1=Victoria    !; 2=Vancouver  !; 3=Prince George;
CA=4=Los Angeles !; 3=Sacramento !; 1=San Francisco;
DE=3=Munchen     !; 1=Berlin     !; 2=Hamburg

Related Topics