KeySort Property

Specifies how Visual FoxPro enumerates through items in a collection when you use the FOR EACH command. Available at design time and run time.

Collection.KeySort [ = nValue ]

Return Value

  • nValue
    The following table lists values for nValue.

    nValue Description

    0

    Index ascending (default)

    1

    Index descending

    2

    Key ascending

    3

    Key descending

Remarks

Applies To: Collection Class

The KeySort property applies only to FOR EACH enumerations.

The KeySort property does not affect how the Add method for collection objects adds items to a collection. Add always adds items by index at the end unless you specify an eBeforeItem or eAfterItem parameter.

You can have a collection in which none of the items have keys. In this case, the last two values you can specify for nValue, 2 and 3, do not apply. If you specify a value of 3, Visual FoxPro uses the value of 1.

Within a collection's FOR EACH loop, changing the KeySort property does not affect the sequencing of items. However, you can nest collection enumerations to change the sequencing. In the following example, the outer FOR EACH loop is sorted using KeySort with a value of 1, and the inner loop is sorted using KeySort with a value of 2:

oCollection.KeySort = 1
FOR EACH oItem IN oCollection
     oCollection.KeySort = 2
     FOR EACH oItem2 IN oCollection
     ENDFOR
ENDFOR

Example

You can set a value for KeySort as shown in the following example:

* Sorts a collection in ascending index order
oCollection.KeySort = 0

See Also

Reference

Collection Object Properties, Methods, and Events
Add Method (Collection Class)
FOR EACH ... ENDFOR Command

Other Resources

Properties (Visual FoxPro)