Skip to content

Commit

Permalink
Set sort order with List.js not sort function
Browse files Browse the repository at this point in the history
  • Loading branch information
javve committed May 10, 2015
1 parent 618565b commit 81d1148
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
1 change: 1 addition & 0 deletions History.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ move form Component to Browserify
add optional id to items
support for data attributes
set default page size to 10000 instead of 200 (because: page size is confusing for new users)
[Breaking] set sort order with List.js not sort function.

### 2014-07
- *[Feature]* Make it possble to add event handlers on init `new List('listId', { searchComplete: function(list) {} })`.
Expand Down
3 changes: 2 additions & 1 deletion src/sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ module.exports = function(list) {

options.sortFunction = options.sortFunction || list.sortFunction;
list.items.sort(function(a, b) {
return options.sortFunction(a, b, options);
var mult = (options.order === 'desc') ? -1 : 1;
return (options.sortFunction(a, b, options) * mult);
});
list.update();
list.trigger('sortComplete');
Expand Down
11 changes: 5 additions & 6 deletions src/utils/natural-sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@ module.exports = function(a, b, opts) {
// normalize spaces; find floats not starting with '0', string or 0 if not defined (Clint Priest)
return (!s.match(ore) || l == 1) && parseFloat(s) || s.replace(snre, ' ').replace(sre, '') || 0;
},
oFxNcL, oFyNcL,
mult = options.desc ? -1 : 1;
oFxNcL, oFyNcL;
// first try and sort Hex codes or Dates
if (yD) {
if ( xD < yD ) { return -1 * mult; }
else if ( xD > yD ) { return 1 * mult; }
if ( xD < yD ) { return -1; }
else if ( xD > yD ) { return 1; }
}
// natural sorting through split numeric strings and default strings
for(var cLoc=0, xNl = xN.length, yNl = yN.length, numS=Math.max(xNl, yNl); cLoc < numS; cLoc++) {
Expand All @@ -42,8 +41,8 @@ module.exports = function(a, b, opts) {
oFxNcL += '';
oFyNcL += '';
}
if (oFxNcL < oFyNcL) { return -1 * mult; }
if (oFxNcL > oFyNcL) { return 1 * mult; }
if (oFxNcL < oFyNcL) { return -1; }
if (oFxNcL > oFyNcL) { return 1; }
}
return 0;
};

0 comments on commit 81d1148

Please sign in to comment.