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 * Represents a single sorter that can be applied to a Store. The sorter is used
17 * to compare two values against each other for the purpose of ordering them. Ordering
18 * is achieved by specifying either:
20 * - {@link #property A sorting property}
21 * - {@link #sorterFn A sorting function}
23 * As a contrived example, we can specify a custom sorter that sorts by rank:
25 * Ext.define('Person', {
26 * extend: 'Ext.data.Model',
27 * fields: ['name', 'rank']
30 * Ext.create('Ext.data.Store', {
34 * sorterFn: function(o1, o2){
35 * var getRank = function(o){
36 * var name = o.get('rank');
37 * if (name === 'first') {
39 * } else if (name === 'second') {
45 * rank1 = getRank(o1),
46 * rank2 = getRank(o2);
48 * if (rank1 === rank2) {
52 * return rank1 < rank2 ? -1 : 1;
67 Ext.define('Ext.util.Sorter', {
70 * @cfg {String} property
71 * The property to sort by. Required unless {@link #sorterFn} is provided. The property is extracted from the object
72 * directly and compared for sorting using the built in comparison operators.
76 * @cfg {Function} sorterFn
77 * A specific sorter function to execute. Can be passed instead of {@link #property}. This sorter function allows
78 * for any kind of custom/complex comparisons. The sorterFn receives two arguments, the objects being compared. The
79 * function should return:
81 * - -1 if o1 is "less than" o2
82 * - 0 if o1 is "equal" to o2
83 * - 1 if o1 is "greater than" o2
88 * Optional root property. This is mostly useful when sorting a Store, in which case we set the root to 'data' to
89 * make the filter pull the {@link #property} out of the data object of each item
93 * @cfg {Function} transform
94 * A function that will be run on each value before it is compared in the sorter. The function will receive a single
95 * argument, the value.
99 * @cfg {String} direction
100 * The direction to sort by.
104 constructor: function(config) {
107 Ext.apply(me, config);
110 if (me.property === undefined && me.sorterFn === undefined) {
111 Ext.Error.raise("A Sorter requires either a property or a sorter function");
115 me.updateSortFunction();
120 * Creates and returns a function which sorts an array by the given property and direction
121 * @return {Function} A function which sorts by the property/direction combination provided
123 createSortFunction: function(sorterFn) {
125 property = me.property,
126 direction = me.direction || "ASC",
127 modifier = direction.toUpperCase() == "DESC" ? -1 : 1;
129 //create a comparison function. Takes 2 objects, returns 1 if object 1 is greater,
130 //-1 if object 2 is greater or 0 if they are equal
131 return function(o1, o2) {
132 return modifier * sorterFn.call(me, o1, o2);
138 * Basic default sorter function that just compares the defined property of each object
140 defaultSorterFn: function(o1, o2) {
142 transform = me.transform,
143 v1 = me.getRoot(o1)[me.property],
144 v2 = me.getRoot(o2)[me.property];
151 return v1 > v2 ? 1 : (v1 < v2 ? -1 : 0);
156 * Returns the root property of the given item, based on the configured {@link #root} property
157 * @param {Object} item The item
158 * @return {Object} The root property of the object
160 getRoot: function(item) {
161 return this.root === undefined ? item : item[this.root];
165 * Set the sorting direction for this sorter.
166 * @param {String} direction The direction to sort in. Should be either 'ASC' or 'DESC'.
168 setDirection: function(direction) {
170 me.direction = direction;
171 me.updateSortFunction();
175 * Toggles the sorting direction for this sorter.
179 me.direction = Ext.String.toggle(me.direction, "ASC", "DESC");
180 me.updateSortFunction();
184 * Update the sort function for this sorter.
185 * @param {Function} [fn] A new sorter function for this sorter. If not specified it will use the default
188 updateSortFunction: function(fn) {
190 fn = fn || me.sorterFn || me.defaultSorterFn;
191 me.sort = me.createSortFunction(fn);