- // private
- sortData : function(f, direction){
- direction = direction || 'ASC';
- var st = this.fields.get(f).sortType;
- var fn = function(r1, r2){
- var v1 = st(r1.data[f]), v2 = st(r2.data[f]);
- return v1 > v2 ? 1 : (v1 < v2 ? -1 : 0);
+ /**
+ * @private
+ * Performs the actual sorting of data. This checks to see if we currently have a multi sort or not. It applies
+ * each sorter field/direction pair in turn by building an OR'ed master sorting function and running it against
+ * the full dataset
+ */
+ sortData : function() {
+ var sortInfo = this.hasMultiSort ? this.multiSortInfo : this.sortInfo,
+ direction = sortInfo.direction || "ASC",
+ sorters = sortInfo.sorters,
+ sortFns = [];
+
+ //if we just have a single sorter, pretend it's the first in an array
+ if (!this.hasMultiSort) {
+ sorters = [{direction: direction, field: sortInfo.field}];
+ }
+
+ //create a sorter function for each sorter field/direction combo
+ for (var i=0, j = sorters.length; i < j; i++) {
+ sortFns.push(this.createSortFunction(sorters[i].field, sorters[i].direction));
+ }
+
+ if (sortFns.length == 0) {
+ return;
+ }
+
+ //the direction modifier is multiplied with the result of the sorting functions to provide overall sort direction
+ //(as opposed to direction per field)
+ var directionModifier = direction.toUpperCase() == "DESC" ? -1 : 1;
+
+ //create a function which ORs each sorter together to enable multi-sort
+ var fn = function(r1, r2) {
+ var result = sortFns[0].call(this, r1, r2);
+
+ //if we have more than one sorter, OR any additional sorter functions together
+ if (sortFns.length > 1) {
+ for (var i=1, j = sortFns.length; i < j; i++) {
+ result = result || sortFns[i].call(this, r1, r2);
+ }
+ }
+
+ return directionModifier * result;