4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../prettify/prettify.js"></script>
8 <style type="text/css">
9 .highlight { display: block; background-color: #ddd; }
11 <script type="text/javascript">
12 function highlight() {
13 document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
17 <body onload="prettyPrint(); highlight();">
18 <pre class="prettyprint lang-js"><span id='Ext-util-Sorter'>/**
19 </span> * @class Ext.util.Sorter
21 * Represents a single sorter that can be applied to a Store
23 Ext.define('Ext.util.Sorter', {
25 <span id='Ext-util-Sorter-cfg-property'> /**
26 </span> * @cfg {String} property The property to sort by. Required unless {@link #sorter} is provided
29 <span id='Ext-util-Sorter-cfg-sorterFn'> /**
30 </span> * @cfg {Function} sorterFn A specific sorter function to execute. Can be passed instead of {@link #property}
33 <span id='Ext-util-Sorter-cfg-root'> /**
34 </span> * @cfg {String} root Optional root property. This is mostly useful when sorting a Store, in which case we set the
35 * root to 'data' to make the filter pull the {@link #property} out of the data object of each item
38 <span id='Ext-util-Sorter-cfg-transform'> /**
39 </span> * @cfg {Function} transform A function that will be run on each value before
40 * it is compared in the sorter. The function will receive a single argument,
44 <span id='Ext-util-Sorter-cfg-direction'> /**
45 </span> * @cfg {String} direction The direction to sort by. Defaults to ASC
47 direction: "ASC",
49 constructor: function(config) {
52 Ext.apply(me, config);
55 if (me.property == undefined && me.sorterFn == undefined) {
56 Ext.Error.raise("A Sorter requires either a property or a sorter function");
60 me.updateSortFunction();
63 <span id='Ext-util-Sorter-method-createSortFunction'> /**
65 * Creates and returns a function which sorts an array by the given property and direction
66 * @return {Function} A function which sorts by the property/direction combination provided
68 createSortFunction: function(sorterFn) {
70 property = me.property,
71 direction = me.direction || "ASC",
72 modifier = direction.toUpperCase() == "DESC" ? -1 : 1;
74 //create a comparison function. Takes 2 objects, returns 1 if object 1 is greater,
75 //-1 if object 2 is greater or 0 if they are equal
76 return function(o1, o2) {
77 return modifier * sorterFn.call(me, o1, o2);
81 <span id='Ext-util-Sorter-method-defaultSorterFn'> /**
83 * Basic default sorter function that just compares the defined property of each object
85 defaultSorterFn: function(o1, o2) {
87 transform = me.transform,
88 v1 = me.getRoot(o1)[me.property],
89 v2 = me.getRoot(o2)[me.property];
96 return v1 > v2 ? 1 : (v1 < v2 ? -1 : 0);
99 <span id='Ext-util-Sorter-method-getRoot'> /**
101 * Returns the root property of the given item, based on the configured {@link #root} property
102 * @param {Object} item The item
103 * @return {Object} The root property of the object
105 getRoot: function(item) {
106 return this.root == undefined ? item : item[this.root];
109 // @TODO: Add docs for these three methods
110 setDirection: function(direction) {
112 me.direction = direction;
113 me.updateSortFunction();
118 me.direction = Ext.String.toggle(me.direction, "ASC", "DESC");
119 me.updateSortFunction();
122 updateSortFunction: function() {
124 me.sort = me.createSortFunction(me.sorterFn || me.defaultSorterFn);