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

Package | spark.collections
Class | public class SortField
Inheritance | SortField SortField flash.events.EventDispatcher
Implements | IAdvancedStyleClient, IFlexModule, IMXMLObject
Subclasses | ComplexSortField, GridSortFieldSimple

Language Version : | ActionScript 3.0

Product Version : | Flex 4.5

Runtime Versions : | Flash Player 10.1, AIR 2.5

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();
         var sortfield:SortField = new SortField("first", true);
         sortfield.setStyle("locale", "en-US");
         sort.fields = [sortfield];
         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();
         var sortfield:SortField = new SortField(null, true);
         sortfield.setStyle("locale", "en-US");
         sort.fields = [sortfield];
         col.sort = sort;

The default comparison provided by the `SortField` class provides correct language specific sorting for strings. The language is selected by the setting the locale style on an instance of the class in one of the following ways:

- By using the class in an MXML declaration and inheriting the locale from the document that contains the declaration.
Example:

      <fx:Declarations>
             <s:SortField id="sf" />
      </fx:Declarations>

- By using an MXML declaration and specifying the locale value in the list of assignments.
Example:

      <fx:Declarations>
          <s:SortField id="sf_SimplifiedChinese" locale="zh-Hans-CN" />
      </fx:Declarations>

- Calling the `setStyle` method, e.g. `sf.setStyle("locale", "zh-Hans-CN")`
- Inheriting the style from a `UIComponent` by calling the UIComponent's `addStyleClient()` method.

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 `<s:SortField>` tag has the following attributes:

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

View the examples

See also

mx.collections.ICollectionView
spark.collections.Sort

Public Properties
Hide Inherited Public Properties
Show Inherited 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
|  | className : String [read-only] | SortField
|  | compareFunction : Function [override] 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
|  | id : String | SortField
|  | inheritingStyles : Object | SortField
|  | moduleFactory : IFlexModuleFactory | SortField
|  | name : String The name of the field to be sorted. | SortField
|  | nonInheritingStyles : Object | 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
|  | styleDeclaration : CSSStyleDeclaration | SortField
|  | styleManager : IStyleManager2 [read-only] | SortField
|  | styleName : Object | SortField
|  | styleParent : IAdvancedStyleClient | SortField
|  | usingCustomCompareFunction : Boolean [read-only] True if this ISortField uses a custom comparator function. | SortField

Public Methods
Hide Inherited Public Methods
Show Inherited Public Methods

| Method | Defined By
--- | --- | ---
|  | SortField(name:String = null, descending:Boolean = false, numeric:Object = null, sortCompareType:String = null, customCompareFunction:Function = null) Constructor. | SortField
|  | clearStyle(styleProp:String):void | SortField
|  | getClassStyleDeclarations():Array | SortField
|  | getStyle(styleProp:String):* | SortField
|  | hasCSSState():Boolean | SortField
|  | initialized(document:Object, id:String):void | 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
|  | matchesCSSState(cssState:String):Boolean | SortField
|  | matchesCSSType(cssType:String):Boolean | SortField
|  | notifyStyleChangeInChildren(styleProp:String, recursive:Boolean):void | SortField
|  | objectHasSortField(object:Object):Boolean | SortField
|  | regenerateStyleCache(recursive:Boolean):void | SortField
|  | registerEffects(effects:Array):void | SortField
|  | reverse():void Reverse the criteria for this sort field. | SortField
|  | setStyle(styleProp:String, newValue:*):void | SortField
|  | styleChanged(styleProp:String):void | SortField
|  | stylesInitialized():void | SortField
|  | updateSortCompareType():Boolean This changes the internal compare function used by the SortField based on the value of sortCompareType. | SortField

Protected Methods
Show Inherited Protected Methods
Show Inherited Protected Methods

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

Styles

| Style | Description | Defined By
--- | --- | --- | ---
|  | locale | Type: String CSS Inheritance: yes The locale identifier that specifies the language, region, script and optionally other related tags and keys. The syntax of this identifier must follow the syntax defined by the Unicode Technical Standard #35 (for example, en-US, de-DE, zh-Hans-CN). For browser based apps, the default locale is based on the language settings from the browser. (Note that this is not the browser UI language that is available from Javascript, but rather is the list of preferred locales for web pages that the user has set in the browser preferences.) For AIR applications, the default UI locale is based on the user's system preferences. See also http://www.unicode.org/reports/tr35/ | SortField

Property Detail

className | property

`className:String` [read-only]

Implementation
`public function get className():String`

compareFunction | property |

`compareFunction:Function`[override]

Language Version : | ActionScript 3.0

Product Version : | Flex 4.5

Runtime Versions : | Flash Player 10.1, AIR 2.5

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. The string comparison is performed using the locale (language, region and script) specific comparison method from the `SortingCollator` class. This class uses the locale style to determine a locale. 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.

Implementation
`public function get compareFunction():Function`
`public function set compareFunction(value:Function):void`

id | property |

`id:String`

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

inheritingStyles | property |

`inheritingStyles:Object`

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

moduleFactory | property |

`moduleFactory:IFlexModuleFactory`

Implementation
`public function get moduleFactory():IFlexModuleFactory`
`public function set moduleFactory(value:IFlexModuleFactory):void`

nonInheritingStyles | property |

`nonInheritingStyles:Object`

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

styleDeclaration | property |

`styleDeclaration:CSSStyleDeclaration`

Implementation
`public function get styleDeclaration():CSSStyleDeclaration`
`public function set styleDeclaration(value:CSSStyleDeclaration):void`

styleManager | property |

`styleManager:IStyleManager2` [read-only]

Implementation
`public function get styleManager():IStyleManager2`

styleName | property |

`styleName:Object`

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

styleParent | property |

`styleParent:IAdvancedStyleClient`

Implementation
`public function get styleParent():IAdvancedStyleClient`
`public function set styleParent(value:IAdvancedStyleClient):void`
Constructor Detail

SortField | () | Constructor

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

Language Version : | ActionScript 3.0

Product Version : | Flex 4.5

Runtime Versions : | Flash Player 10.1, AIR 2.5

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`.
| `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

clearStyle | () | method

`public function clearStyle(styleProp:String):void`

Parameters

| `styleProp:String`

getClassStyleDeclarations | () | method |

`public function getClassStyleDeclarations():Array`

Returns

| `Array`

getStyle | () | method |

`public function getStyle(styleProp:String):*`

Parameters

| `styleProp:String`

Returns

| `*`

hasCSSState | () | method |

`public function hasCSSState():Boolean`

Returns

| `Boolean`

initialized | () | method |

`public function initialized(document:Object, id:String):void`

Parameters

| `document:Object`
| `id:String`

matchesCSSState | () | method |

`public function matchesCSSState(cssState:String):Boolean`

Parameters

| `cssState:String`

Returns

| `Boolean`

matchesCSSType | () | method |

`public function matchesCSSType(cssType:String):Boolean`

Parameters

| `cssType:String`

Returns

| `Boolean`

notifyStyleChangeInChildren | () | method |

`public function notifyStyleChangeInChildren(styleProp:String, recursive:Boolean):void`

Parameters

| `styleProp:String`
| `recursive:Boolean`

regenerateStyleCache | () | method |

`public function regenerateStyleCache(recursive:Boolean):void`

Parameters

| `recursive:Boolean`

registerEffects | () | method |

`public function registerEffects(effects:Array):void`

Parameters

| `effects:Array`

setStyle | () | method |

`public function setStyle(styleProp:String, newValue:*):void`

Parameters

| `styleProp:String`
| `newValue:*`

stringCompare | () | method |

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

Language Version : | ActionScript 3.0

Product Version : | Flex 4.5

Runtime Versions : | Flash Player 10.1, AIR 2.5

Pull the strings from the objects and call the implementation.

Parameters

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

Returns

| `int`

styleChanged | () | method |

`public function styleChanged(styleProp:String):void`

Parameters

| `styleProp:String`

stylesInitialized | () | method |

`public function stylesInitialized():void`

xmlCompare | () | method |

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

Language Version : | ActionScript 3.0

Product Version : | Flex 4.5

Runtime Versions : | Flash Player 10.1, AIR 2.5

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`

Examples
SortExample1.mxml

    <?xml version="1.0" encoding="utf-8"?>
    <!--

      Licensed to the Apache Software Foundation (ASF) under one or more
      contributor license agreements.  See the NOTICE file distributed with
      this work for additional information regarding copyright ownership.
      The ASF licenses this file to You under the Apache License, Version 2.0
      (the "License"); you may not use this file except in compliance with
      the License.  You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

      Unless required by applicable law or agreed to in writing, software
      distributed under the License is distributed on an "AS IS" BASIS,
      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      See the License for the specific language governing permissions and
      limitations under the License.

    -->
    <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
                   xmlns:s="library://ns.adobe.com/flex/spark"
                   xmlns:mx="library://ns.adobe.com/flex/mx"
                   minWidth="955" minHeight="600">
        <!-- Sample program for spark.collections.Sort and SortField -->
        <fx:Declarations>
            <s:Sort id="sortbyLastName_FirstName">
                <s:fields>
                    <s:SortField name="last"/>
                    <s:SortField name="first"/>
                </s:fields>
            </s:Sort>
            <mx:ArrayCollection id="collection" sort="{sortbyLastName_FirstName}">
                <mx:source>
                    <fx:Object first="Anders" last="Dickerson"/>
                    <fx:Object first="Eileen" last="Maccormick"/>
                    <fx:Object first="Aiden" last="MacCormick"/>
                    <fx:Object first="Steve" last="MacGregor"/>
                </mx:source>
            </mx:ArrayCollection>
        </fx:Declarations>

        <s:VGroup>
            <s:VGroup>
                <s:HGroup>
                    <s:Label text="Input Locale ID Name: "/>
                    <s:TextInput id="inputLocaleIDName"/>
                    <!--
                    Sets the locale style on the document UI component.
                    The SortField and Sort objects defined in the
                    fx:Declarations section will inherit this style.
                    -->
                    <s:Button click="{setStyle('locale', inputLocaleIDName.text);
                              collection.refresh()}" label="Apply"/>
                </s:HGroup>
                <s:Label text="Example: 'en-US', 'fr-FR', 'zh-CN', 'ar-SA'"/>
            </s:VGroup>
            <s:DataGrid dataProvider="{collection}" width="100%"
                         creationComplete="{collection.refresh()}">
                <s:columns>
                    <s:ArrayList>
                        <s:GridColumn dataField="last"/>
                        <s:GridColumn dataField="first"/>
                    </s:ArrayList>
                </s:columns>
            </s:DataGrid>
        </s:VGroup>
    </s:Application>

SortExample2.mxml

    <?xml version="1.0" encoding="utf-8"?>
    <!--

      Licensed to the Apache Software Foundation (ASF) under one or more
      contributor license agreements.  See the NOTICE file distributed with
      this work for additional information regarding copyright ownership.
      The ASF licenses this file to You under the Apache License, Version 2.0
      (the "License"); you may not use this file except in compliance with
      the License.  You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

      Unless required by applicable law or agreed to in writing, software
      distributed under the License is distributed on an "AS IS" BASIS,
      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      See the License for the specific language governing permissions and
      limitations under the License.

    -->
    <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
                   xmlns:s="library://ns.adobe.com/flex/spark"
                   xmlns:mx="library://ns.adobe.com/flex/mx"
                   minWidth="955" minHeight="600"
                   creationComplete="initApp()">
        <!-- Sample program for spark.collections.Sort and SortField -->
        <fx:Script>
            <![CDATA[
                import mx.collections.ArrayCollection;
                import spark.collections.*;
                [Bindable]
                private var collection:ArrayCollection = new ArrayCollection();

                private function localeChange():void
                {
                    // Sets the locale style on this application.
                    // The SortField objects will inherit this style.
                    setStyle('locale', inputLocaleIDName.text);
                    collection.refresh();
                }

                private function initApp() : void
                {
                    // Add data to the collection.
                    collection.addItem({first:"Anders", last:"Dickerson"});
                    collection.addItem({first:"Steve", last:"Maccormick"});
                    collection.addItem({first:"Aiden", last:"MacCormick"});
                    collection.addItem({first:"Eileen", last:"MacGregor"});

                    // Create the Sort instance.
                    var sort:Sort = new Sort();

                    // Set the sort field; sort on the last name first, first name
                    // second.
                    var sortfieldLastName:SortField = new SortField("last",true);
                    var sortfieldFirstName:SortField = new SortField("first",true);
                    sort.fields = [sortfieldLastName, sortfieldFirstName];

                    // Add the sort field objects to this application's list of
                    // style clients.  This will cause the sort field objects to
                    // inherit the locale style from this Application.
                    addStyleClient(sortfieldLastName);
                    addStyleClient(sortfieldFirstName);

                    // Assign the Sort object to the view.
                    collection.sort = sort;

                    // Apply the sort to the collection.
                    collection.refresh();
                }
            ]]>
        </fx:Script>

        <s:VGroup>
            <s:VGroup>
                <s:HGroup>
                    <s:Label text="Input Locale ID Name: "/>
                    <s:TextInput id="inputLocaleIDName"/>
                    <s:Button click="localeChange()" label="Apply"/>
                </s:HGroup>
                <s:Label text="Example: 'en-US', 'fr-FR', 'zh-CN', 'ar-SA'"/>
            </s:VGroup>

            <s:DataGrid dataProvider="{collection}" width="100%"
                         creationComplete="{collection.refresh()}">
                <s:columns>
                    <s:ArrayList>
                        <s:GridColumn dataField="last"/>
                        <s:GridColumn dataField="first"/>
                    </s:ArrayList>
                </s:columns>
            </s:DataGrid>
        </s:VGroup>
    </s:Application>

Wed Nov 15 2017, 09:50 AM +01:00