Merge pull request #281 from ryantenney/master

Array Shuffle
This commit is contained in:
Jeremy Ashkenas
2011-10-04 14:11:30 -07:00
3 changed files with 34 additions and 1 deletions

View File

@@ -239,6 +239,21 @@
return result.value;
};
// Shuffle an array.
_.shuffle = function(obj) {
var shuffled = [], rand;
each(obj, function(value, index, list) {
if (index == 0) {
shuffled[0] = value;
} else {
rand = Math.floor(Math.random() * (index + 1));
shuffled[index] = shuffled[rand];
shuffled[rand] = value;
}
});
return shuffled;
};
// Sort the object's values by a criterion produced by an iterator.
_.sortBy = function(obj, iterator, context) {
return _.pluck(_.map(obj, function(value, index, list) {