Apache Flex 4.16.0 API Reference | All Packages | All Classes | Index | Frames No Frames |
SortField

Package | mx.collections
Class | public class SortField
Inheritance | SortField flash.events.EventDispatcher
Implements | ISortField
Subclasses | SortField

Language Version : | ActionScript 3.0

Product Version : | Flex 3

Runtime Versions : | Flash Player 9, AIR 1.1

| Starting with Flex 4.5, Apache recommends that you use the spark.collections.SortField class as an alternative to this class.

Provides the sorting information required to establish a sort on a field or property in a collection view. The SortField class is meant to be used with the Sort class. Typically the sort is defined for collections of complex items, that is items in which the sort is performed on properties of those objects. As in the following example:

         var col:ICollectionView = new ArrayCollection();
         col.addItem({first:"Anders", last:"Dickerson"});
         var sort:Sort = new Sort();
         sort.fields = [new SortField("first", true)];
         col.sort = sort;

There are situations in which the collection contains simple items, like `String`, `Date`, `Boolean`, etc. In this case, sorting should be applied to the simple type directly. When constructing a sort for this situation only a single sort field is required and should not have a `name` specified. For example:

         var col:ICollectionView = new ArrayCollection();
         col.addItem("California");
         col.addItem("Arizona");
         var sort:Sort = new Sort();
         sort.fields = [new SortField(null, true)];
         col.sort = sort;

By default the comparison provided by the SortField class does not provide correct language specific sorting for strings. For this type of sorting please see the `spark.collections.Sort` and `spark.collections.SortField` classes.

Note: to prevent problems like FLEX-34853 it is recommended to use SortField instances as immutable objects (by not changing their state).

MXML Syntax Show MXML Syntax
Hide MXML Syntax

The `<mx:SortField>` tag has the following attributes:

      <mx:SortField
      Properties
      caseInsensitive="false"
      compareFunction="Internal compare function"
      descending="false"
      name="null"
      numeric="null"
      />

See also

mx.collections.ICollectionView
mx.collections.Sort
spark.collections.Sort
spark.collections.SortField

Public Properties

| Property | Defined By
--- | --- | ---
|  | arraySortOnOptions : int [read-only] This helper property is used internally by the findItem() and sort() methods. | SortField
|  | caseInsensitive : Boolean [read-only] Specifies whether the sort for this field should be case insensitive. | SortField
|  | compareFunction : Function The function that compares two items during a sort of items for the associated collection. | SortField
|  | descending : Boolean Specifies whether this field should be sorted in descending order. | SortField
|  | name : String The name of the field to be sorted. | SortField
|  | numeric : Object Specifies that if the field being sorted contains numeric (number/int/uint) values, or string representations of numeric values, the comparator use a numeric comparison. | SortField
|  | sortCompareType : String Specifies what compare type will be used for the sortField. | SortField
|  | usingCustomCompareFunction : Boolean [read-only] True if this ISortField uses a custom comparator function. | SortField

Public Methods

| Method | Defined By
--- | --- | ---
|  | SortField(name:String = null, caseInsensitive:Boolean = false, descending:Boolean = false, numeric:Object = null, sortCompareType:String = null, customCompareFunction:Function = null) Constructor. | SortField
|  | initializeDefaultCompareFunction(obj:Object):void A helper function called by the Sort class to set the default comparison function to perform a comparison based on one of three things: whether or not a custom compare function has been set, the data type for the specified field or the the value of the numeric property. | SortField
|  | objectHasSortField(object:Object):Boolean | SortField
|  | reverse():void Reverse the criteria for this sort field. | SortField
|  | updateSortCompareType():Boolean This changes the internal compare function used by the SortField based on the value of sortCompareType. | SortField

Protected Methods

| Method | Defined By
--- | --- | ---
|  | getSortFieldValue(obj:Object):* | SortField
|  | stringCompare(a:Object, b:Object):int Pull the strings from the objects and call the implementation. | SortField
|  | xmlCompare(a:Object, b:Object):int Pull the values out fo the XML object, then compare using the string or numeric comparator depending on the numeric flag. | SortField

Property Detail

arraySortOnOptions | property

`arraySortOnOptions:int` [read-only]

Language Version : | ActionScript 3.0

Product Version : | Flex 3

Runtime Versions : | Flash Player 9, AIR 1.1

This helper property is used internally by the `findItem()` and `sort()` methods. Other uses of this property are not supported. Returns -1 if this ISortField shouldn't be used by the `Sort` class to sort the field (there is no compareFunction or no name). Otherwise, returns a bitmask of sort options.

Implementation
`public function get arraySortOnOptions():int`

caseInsensitive | property |

`caseInsensitive:Boolean` [read-only]

Language Version : | ActionScript 3.0

Product Version : | Flex 3

Runtime Versions : | Flash Player 9, AIR 1.1

Specifies whether the sort for this field should be case insensitive.

The default value is `false`.

This property can be used as the source for data binding.

Implementation
`public function get caseInsensitive():Boolean`

compareFunction | property |

`compareFunction:Function`

Language Version : | ActionScript 3.0

Product Version : | Flex 3

Runtime Versions : | Flash Player 9, AIR 1.1

The function that compares two items during a sort of items for the associated collection. If you specify a `compareFunction` property in an `ISort` object, Flex ignores any `compareFunction` properties of the ISort's `SortField` objects.

The compare function must have the following signature:

`function myCompare(a:Object, b:Object):int`

This function must return the following values:

- -1, if the `Object a` should appear before the `Object b` in the sorted sequence
- 0, if the `Object a` equals the `Object b`
- 1, if the `Object a` should appear after the `Object b` in the sorted sequence

The default value is an internal compare function that can perform a string, numeric, or date comparison in ascending or descending order, with case-sensitive or case-insensitive string comparisons. Specify your own function only if you need a need a custom comparison algorithm. This is normally only the case if a calculated field is used in a display.

Note if you need language-specific sorting then consider using the `spark.collections.SortField` class.
Implementation
`public function get compareFunction():Function`
`public function set compareFunction(value:Function):void`

See also

spark.collections.SortField

descending | property |

`descending:Boolean`

Language Version : | ActionScript 3.0

Product Version : | Flex 3

Runtime Versions : | Flash Player 9, AIR 1.1

Specifies whether this field should be sorted in descending order.

The default value is `false` (ascending).

This property can be used as the source for data binding.

Implementation
`public function get descending():Boolean`
`public function set descending(value:Boolean):void`

name | property |

`name:String`

Language Version : | ActionScript 3.0

Product Version : | Flex 3

Runtime Versions : | Flash Player 9, AIR 1.1

The name of the field to be sorted.

The default value is `null`.

This property can be used as the source for data binding.

Implementation
`public function get name():String`
`public function set name(value:String):void`

numeric | property |

`numeric:Object`

Language Version : | ActionScript 3.0

Product Version : | Flex 3

Runtime Versions : | Flash Player 9, AIR 1.1

Specifies that if the field being sorted contains numeric (`number/int/uint`) values, or string representations of numeric values, the comparator use a numeric comparison.

This property is used by `SortField` class in case custom compare function is not provided.

If this property is `true`, the built-in numeric compare function is used. Each of data items is cast to a `Number()` function before the comparison.

If this property is `false`, the built-in string compare function is used. Each of data items is cast to a `String()` function before the comparison.

If this property is `null`, the first data item is introspected to see if it is a number or string and the sort proceeds based on that introspection.

The default value is `null`.

This property can be used as the source for data binding.

Implementation
`public function get numeric():Object`
`public function set numeric(value:Object):void`

sortCompareType | property |

`sortCompareType:String`

Language Version : | ActionScript 3.0

Product Version : | Flex 4.11

Runtime Versions : | Flash Player 11.8, AIR 3.8

Specifies what compare type will be used for the sortField. This overrides the default behavior.

This property can be used as the source for data binding.

Implementation
`public function get sortCompareType():String`
`public function set sortCompareType(value:String):void`

usingCustomCompareFunction | property |

`usingCustomCompareFunction:Boolean` [read-only]

Language Version : | ActionScript 3.0

Product Version : | Flex 3

Runtime Versions : | Flash Player 9, AIR 1.1

True if this `ISortField` uses a custom comparator function.

Implementation
`public function get usingCustomCompareFunction():Boolean`
Constructor Detail

SortField | () | Constructor

`public function SortField(name:String = null, caseInsensitive:Boolean = false, descending:Boolean = false, numeric:Object = null, sortCompareType:String = null, customCompareFunction:Function = null)`

Language Version : | ActionScript 3.0

Product Version : | Flex 3

Runtime Versions : | Flash Player 9, AIR 1.1

Constructor.

Parameters

| `name:String` (default = `null`)`` — The name of the property that this field uses for comparison. If the object is a simple type, pass `null`.
| `caseInsensitive:Boolean` (default = `false`)`` — When sorting strings, tells the comparator whether to ignore the case of the values.
| `descending:Boolean` (default = `false`)`` — Tells the comparator whether to arrange items in descending order.
| `numeric:Object` (default = `null`)`` — Tells the comparator whether to compare sort items as numbers, instead of alphabetically.
| `sortCompareType:String` (default = `null`)`` — Gives an indication to SortField which of the default compare functions to use.
| `customCompareFunction:Function` (default = `null`)`` — Use a custom function to compare the objects based on this SortField.

Method Detail

getSortFieldValue | () | method

`protected function getSortFieldValue(obj:Object):*`

Parameters

| `obj:Object`

Returns

| `*`

initializeDefaultCompareFunction | () | method |

`public function initializeDefaultCompareFunction(obj:Object):void`

Language Version : | ActionScript 3.0

Product Version : | Flex 3

Runtime Versions : | Flash Player 9, AIR 1.1

A helper function called by the `Sort` class to set the default comparison function to perform a comparison based on one of three things: whether or not a custom compare function has been set, the data type for the specified field or the the value of the numeric property. If the the `numeric` property is true, then a numeric comparison will be performed when sorting.

Parameters

| `obj:Object` — The object that contains the data. If the field name has been set with the name property, then the name will be used to access the data value from this object. Otherwise the object itself will be used as the data value.

objectHasSortField | () | method |

`public function objectHasSortField(object:Object):Boolean`

Parameters

| `object:Object`

Returns

| `Boolean`

reverse | () | method |

`public function reverse():void`

Language Version : | ActionScript 3.0

Product Version : | Flex 3

Runtime Versions : | Flash Player 9, AIR 1.1

Reverse the criteria for this sort field. If the field was sorted in descending order, for example, sort it in ascending order.

NOTE: An `ICollectionView` does not automatically update when the `ISortFields` are modified; call its `refresh()` method to update the view.

Note: a future release of Apache Flex SDK will change the signature of this function to return a reversed clone of this SortField instance. See FLEX-34853 for more details.

stringCompare | () | method |

`protected function stringCompare(a:Object, b:Object):int`

Language Version : | ActionScript 3.0

Product Version : | Flex 3

Runtime Versions : | Flash Player 9, AIR 1.1

Pull the strings from the objects and call the implementation.

Parameters

| `a:Object`
| `b:Object`

Returns

| `int`

updateSortCompareType | () | method |

`public function updateSortCompareType():Boolean`

Language Version : | ActionScript 3.0

Product Version : | Flex 4.11

Runtime Versions : | Flash Player 11.8, AIR 3.8

This changes the internal compare function used by the `SortField` based on the value of `sortCompareType`.

Returns

| `Boolean` — true for successfully matched or false for failure to match the `sortCompareType`.

xmlCompare | () | method |

`protected function xmlCompare(a:Object, b:Object):int`

Language Version : | ActionScript 3.0

Product Version : | Flex 3

Runtime Versions : | Flash Player 9, AIR 1.1

Pull the values out fo the XML object, then compare using the string or numeric comparator depending on the numeric flag.

Parameters

| `a:Object`
| `b:Object`

Returns

| `int`

Sun Mar 5 2017, 03:42 PM +11:00