Skip to content

fortnightlabs/jquery-sort-by

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 

Repository files navigation

Sometimes the ruby-style sort_by sorting is easier to use than JavaScripts default sort implementation.

Here's a plugin that brings sort_by functionality to jQuery.

$.fn.sortBy = function(fn, options) {
  var opts = $.extend({ reverse: false }, options);
  return this.map(function(i, el) { return [[fn(el, i), el]]; }).sort(function(a, b) {
    return (a[0] == b[0] ? 0 : (a[0] > b[0] ? 1 : -1)) * (opts.reverse ? -1 : 1);
  }).map(function(i, ar) { return ar[1]; });
};

And here's how you use it.

$trs.sortBy(function(tr) {
  return $(tr).find('.' + col).data('sort');
}, { reverse: $th.is('.desc') }).appendTo($tbody);

About

Ruby-style sort_by functionality in jQuery.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published