3
0
mirror of https://github.com/reality/dbot.git synced 2024-11-27 14:29:29 +01:00

make an object sort function in snippets

This commit is contained in:
Luke Slater 2012-06-24 23:58:22 +01:00
parent 27bce53e2e
commit a992e65b6e
3 changed files with 14 additions and 19 deletions

View File

@ -26,17 +26,10 @@ var kick = function(dbot) {
// who have kicked other people the most.
'~kickstats': function(event) {
var orderedKickLeague = function(list, topWhat) {
var kickArr = [];
for(var kickUser in list) {
if(list.hasOwnProperty(kickUser)) {
kickArr.push([kickUser, list[kickUser]]);
}
}
kickArr = kickArr.sort(function(a, b) { return a[1] - b[1]; });
var kickArr = Object.prototype.sort(list, function(key, obj) { return obj[key]; });
kickArr = kickArr.slice(kickArr.length - 10).reverse();
var kickString = "Top " + topWhat + ": ";
var kickString = "Top " + topWhat + ": ";
for(var i=0;i<kickArr.length;i++) {
kickString += kickArr[i][0] + " (" + kickArr[i][1] + "), ";
}

View File

@ -57,18 +57,10 @@ var quotes = function(dbot) {
// Shows a list of the biggest categories
'~qstats': function(event) {
var qSizes = [];
for(var cat in quotes) {
if(quotes[cat].length != 0) {
qSizes.push([cat, quotes[cat].length]);
}
}
qSizes = qSizes.sort(function(a, b) { return a[1] - b[1]; });
var qSsizes = Object.prototype.sort(quotes, function(key, obj) { obj[key].length });
qSizes = qSizes.slice(qSizes.length - 10).reverse();
var qString = dbot.t('large_categories');
for(var i=0;i<qSizes.length;i++) {
qString += qSizes[i][0] + " (" + qSizes[i][1] + "), ";
}

View File

@ -178,7 +178,17 @@ Object.prototype.length = function() {
if(this.hasOwnProperty(key))
l++;
return l;
}
};
Object.prototype.sort = function(object, scorer) {
var sortArr = [];
for(var key in object) {
if(object.hasOwnProperty(key)) {
sortArr.push([key, scorer(key, object)]);
}
}
return sortArr.sort(function(a, b) { return a[1] - b[1]; });
};
/*** Integer ***/