Skip to content

Instantly share code, notes, and snippets.

@ihr
Created October 29, 2012 10:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ihr/3972915 to your computer and use it in GitHub Desktop.
Save ihr/3972915 to your computer and use it in GitHub Desktop.
Second sort criterion for a backbone.paginator.clientPager
_sort: function ( models, sort, direction ) {
               models = models.sort(function (a, b) {
                   var ac = a.get(sort),
                       bc = b.get(sort);
                   if ( !ac || !bc ) {
                       return 0;
                   } else {
                       ac = ac.toString().toLowerCase();
                       bc = bc.toString().toLowerCase();
                   }
                   if (direction === 'desc') {
                       if((!ac.match(/[^\d\.]/) && ac.match(/[\d\.]*/)) &&
                           (!bc.match(/[^\d\.]/) && bc.match(/[\d\.]*/))
                           ){
                           if( (ac - 0) < (bc - 0) ) {
                               return 1;
                           }
                           if( (ac - 0) > (bc - 0) ) {
                               return -1;
                           }
                       } else {
                           if (ac < bc) {
                               return 1;
                           }
                           if (ac > bc) {
                               return -1;
                           }
                       }
                   } else {
                       if((!ac.match(/[^\d\.]/) && ac.match(/[\d\.]*/)) &&
                           (!bc.match(/[^\d\.]/) && bc.match(/[\d\.]*/))
                           ){
                           if( (ac - 0) < (bc - 0) ) {
                               return -1;
                           }
                           if( (ac - 0) > (bc - 0) ) {
                               return 1;
                           }
                       } else {
                           if (ac < bc) {
                               return -1;
                           }
                           if (ac > bc) {
                               return 1;
                           }
                       }
                   }
/* BEGIN Code added */
                   ac = a.get('unique_id');
                   bc = b.get('unique_id');
                   if (ac < bc) {
                       return -1;
                   }
                   else if (ac > bc) {
                       return 1;
                   }
/* END Code added */
                   return 0;
               });
               return models;
           }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment