3 This file is part of Ext JS 4
5 Copyright (c) 2011 Sencha Inc
7 Contact: http://www.sencha.com/contact
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file. Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
16 * @class Ext.util.Sorter
19 Represents a single sorter that can be applied to a Store. The sorter is used
20 to compare two values against each other for the purpose of ordering them. Ordering
21 is achieved by specifying either:
22 - {@link #property A sorting property}
23 - {@link #sorterFn A sorting function}
25 As a contrived example, we can specify a custom sorter that sorts by rank:
27 Ext.define('Person', {
28 extend: 'Ext.data.Model',
29 fields: ['name', 'rank']
32 Ext.create('Ext.data.Store', {
36 sorterFn: function(o1, o2){
37 var getRank = function(o){
38 var name = o.get('rank');
39 if (name === 'first') {
41 } else if (name === 'second') {
50 if (rank1 === rank2) {
54 return rank1 < rank2 ? -1 : 1;
71 Ext.define('Ext.util.Sorter', {
74 * @cfg {String} property The property to sort by. Required unless {@link #sorterFn} is provided.
75 * The property is extracted from the object directly and compared for sorting using the built in
76 * comparison operators.
80 * @cfg {Function} sorterFn A specific sorter function to execute. Can be passed instead of {@link #property}.
81 * This sorter function allows for any kind of custom/complex comparisons.
82 * The sorterFn receives two arguments, the objects being compared. The function should return:
84 * <li>-1 if o1 is "less than" o2</li>
85 * <li>0 if o1 is "equal" to o2</li>
86 * <li>1 if o1 is "greater than" o2</li>
91 * @cfg {String} root Optional root property. This is mostly useful when sorting a Store, in which case we set the
92 * root to 'data' to make the filter pull the {@link #property} out of the data object of each item
96 * @cfg {Function} transform A function that will be run on each value before
97 * it is compared in the sorter. The function will receive a single argument,
102 * @cfg {String} direction The direction to sort by. Defaults to ASC
106 constructor: function(config) {
109 Ext.apply(me, config);
112 if (me.property === undefined && me.sorterFn === undefined) {
113 Ext.Error.raise("A Sorter requires either a property or a sorter function");
117 me.updateSortFunction();
122 * Creates and returns a function which sorts an array by the given property and direction
123 * @return {Function} A function which sorts by the property/direction combination provided
125 createSortFunction: function(sorterFn) {
127 property = me.property,
128 direction = me.direction || "ASC",
129 modifier = direction.toUpperCase() == "DESC" ? -1 : 1;
131 //create a comparison function. Takes 2 objects, returns 1 if object 1 is greater,
132 //-1 if object 2 is greater or 0 if they are equal
133 return function(o1, o2) {
134 return modifier * sorterFn.call(me, o1, o2);
140 * Basic default sorter function that just compares the defined property of each object
142 defaultSorterFn: function(o1, o2) {
144 transform = me.transform,
145 v1 = me.getRoot(o1)[me.property],
146 v2 = me.getRoot(o2)[me.property];
153 return v1 > v2 ? 1 : (v1 < v2 ? -1 : 0);
158 * Returns the root property of the given item, based on the configured {@link #root} property
159 * @param {Object} item The item
160 * @return {Object} The root property of the object
162 getRoot: function(item) {
163 return this.root === undefined ? item : item[this.root];
167 * Set the sorting direction for this sorter.
168 * @param {String} direction The direction to sort in. Should be either 'ASC' or 'DESC'.
170 setDirection: function(direction) {
172 me.direction = direction;
173 me.updateSortFunction();
177 * Toggles the sorting direction for this sorter.
181 me.direction = Ext.String.toggle(me.direction, "ASC", "DESC");
182 me.updateSortFunction();
186 * Update the sort function for this sorter.
187 * @param {Function} fn (Optional) A new sorter function for this sorter. If not specified it will use the
188 * default sorting function.
190 updateSortFunction: function(fn) {
192 fn = fn || me.sorterFn || me.defaultSorterFn;
193 me.sort = me.createSortFunction(fn);