Merge branch 'hotfix/21.56.9' into master
[gitter.git] / shared / sorting / user-sort.js
blob98d063c2e2edc0a6880ca8863afdacc2aa556397
1 'use strict';
2 var ensurePojo = require('./ensure-pojo');
3 // @const - higher index in array, higher rank
4 var RANK = ['contributor', 'admin'];
6 function compareRank(a, b) {
7 return RANK.indexOf(a.role) - RANK.indexOf(b.role) || 0;
10 function compareNames(a, b) {
11 return b.username.toLowerCase().localeCompare(a.username.toLowerCase());
14 // it is worth noticing that we want to sort in a descindencing order, thus the negative results
15 module.exports = function(a, b) {
16 // normalizing Backbone.Model to POJO
17 a = ensurePojo(a);
18 b = ensurePojo(b);
20 var rankDifference = compareRank(a, b); // checks if there is rank difference
22 // attempts to sort by rank
23 if (rankDifference) {
24 return -rankDifference;
27 // default sort
28 return -compareNames(a, b);