From 4a3255d4fd8e68a89455b6c960f8a50611577fa8 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Wed, 21 Jan 2015 17:41:43 -0500 Subject: [PATCH] search sorting --- public/src/client/groups/list.js | 6 ++++-- src/groups.js | 23 ++++++++++++++++++++++- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/public/src/client/groups/list.js b/public/src/client/groups/list.js index 06ce88ade9..45c5ce5a97 100644 --- a/public/src/client/groups/list.js +++ b/public/src/client/groups/list.js @@ -39,12 +39,14 @@ define('forum/groups/list', function() { Groups.search = function() { var groupsEl = $('#groups-list'), - queryEl = $('#search-text'); + queryEl = $('#search-text'), + sortEl = $('#search-sort'); socket.emit('groups.search', { query: queryEl.val(), options: { - expand: true + expand: true, + sort: sortEl.val() } }, function(err, groups) { templates.parse('partials/groups/list', { diff --git a/src/groups.js b/src/groups.js index f62fda1f43..2c510fc119 100644 --- a/src/groups.js +++ b/src/groups.js @@ -892,8 +892,29 @@ var async = require('async'), async.mapLimit(groupNames, 5, function(groupName, next) { Groups.get(groupName, options || {}, next); }, next); - } + }, + async.apply(Groups.sort, options.sort) ], callback); }; + Groups.sort = function(strategy, groups, next) { + switch(strategy) { + case 'count': + groups = groups.sort(function(a, b) { + return a.slug > b.slug; + }).sort(function(a, b) { + return a.memberCount < b.memberCount; + }); + break; + + case 'alpha': // intentional fall-through + default: + groups = groups.sort(function(a, b) { + return a.slug > b.slug; + }); + } + + next(null, groups); + }; + }(module.exports));