-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Gerad Suyderhoud
committed
Nov 12, 2010
0 parents
commit 2120b6a
Showing
1 changed file
with
16 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
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); |