From 6e194093be40db00cafde13fe7ab3ae556a213c1 Mon Sep 17 00:00:00 2001 From: Dustin Goodman Date: Wed, 22 Jan 2014 23:02:03 -0500 Subject: [PATCH 1/2] update Sort Service - keep nulls at bottom of list regardless of sort order - if user provides custom sort, allow them to have full control over sort --- src/services/SortService.js | 44 +++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/src/services/SortService.js b/src/services/SortService.js index 50dc946b93..e7638f31dd 100644 --- a/src/services/SortService.js +++ b/src/services/SortService.js @@ -1,6 +1,7 @@ angular.module('ngGrid.services').factory('$sortService', ['$parse', function($parse) { var sortService = {}; sortService.colSortFnCache = {}; // cache of sorting functions. Once we create them, we don't want to keep re-doing it + sortService.isCustomSort = false; // track if we're using an internal sort or a user provided sort // this takes an piece of data from the cell and tries to determine its type and what sorting // function to use for it // @item - the cell data @@ -98,6 +99,7 @@ data.sort(function (itemA, itemB) { var tem = 0, indx = 0, + res, sortFn; while (tem === 0 && indx < l) { // grab the metadata for the rest of the logic @@ -107,30 +109,33 @@ var propA = $parse(order[indx])(itemA); var propB = $parse(order[indx])(itemB); - // we want to allow zero values to be evaluated in the sort function - if ((!propA && propA !== 0) || (!propB && propB !== 0)) { - // we want to force nulls and such to the bottom when we sort... which effectively is "greater than" - if (!propB && !propA) { - tem = 0; + // if user provides custom sort, we want them to have full control of the sort + if (sortService.isCustomSort) { + res = sortFn(propA, propB); + tem = direction === ASC ? res: 0 - res; + } else { + // we want to allow zero values to be evaluated in the sort function + if ((!propA && propA !== 0) || (!propB && propB !== 0)) { + // we want to force nulls and such to the bottom when we sort... which effectively is "greater than" + if (!propB && !propA) { + tem = 0; + } + else if (!propA) { + tem = 1; + } + else if (!propB) { + tem = -1; + } } - else if (!propA) { - tem = 1; + else { + // this will keep nulls at the bottom regardless of ordering + res = sortFn(propA, propB); + tem = direction === ASC ? res: 0 - res; } - else if (!propB) { - tem = -1; - } - } - else { - tem = sortFn(propA, propB); } indx++; } - //made it this far, we don't have to worry about null & undefined - if (direction === ASC) { - return tem; - } else { - return 0 - tem; - } + return tem; }); }; sortService.Sort = function(sortInfo, data) { @@ -150,6 +155,7 @@ else if (col.sortingAlgorithm !== undefined) { sortFn = col.sortingAlgorithm; sortService.colSortFnCache[col.field] = col.sortingAlgorithm; + sortService.isCustomSort = true; } else { // try and guess what sort function to use item = data[0]; From aff6a2e4c45640c3c25cd272ccdaeda6bd6ea7ce Mon Sep 17 00:00:00 2001 From: Dustin Goodman Date: Thu, 23 Jan 2014 15:30:47 -0500 Subject: [PATCH 2/2] fix spacing --- src/services/SortService.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/services/SortService.js b/src/services/SortService.js index e7638f31dd..d1c03c2763 100644 --- a/src/services/SortService.js +++ b/src/services/SortService.js @@ -112,7 +112,7 @@ // if user provides custom sort, we want them to have full control of the sort if (sortService.isCustomSort) { res = sortFn(propA, propB); - tem = direction === ASC ? res: 0 - res; + tem = direction === ASC ? res : 0 - res; } else { // we want to allow zero values to be evaluated in the sort function if ((!propA && propA !== 0) || (!propB && propB !== 0)) { @@ -130,7 +130,7 @@ else { // this will keep nulls at the bottom regardless of ordering res = sortFn(propA, propB); - tem = direction === ASC ? res: 0 - res; + tem = direction === ASC ? res : 0 - res; } } indx++;