Added function shuffle, with test case.

This commit is contained in:
Ryan W Tenney
2011-08-25 21:44:29 +00:00
parent 610b347174
commit f4cba513b9
3 changed files with 44 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) {