From 0c0635d766f61eb25db52e4e11706932185e416d Mon Sep 17 00:00:00 2001
From: George Goldberg <george@gberg.me>
Date: Fri, 6 Jan 2017 14:03:11 +0000
Subject: PLT-4614 Fix ordering of channels. (#4980)

---
 webapp/utils/channel_utils.jsx | 20 ++++++++++++--------
 1 file changed, 12 insertions(+), 8 deletions(-)

(limited to 'webapp/utils/channel_utils.jsx')

diff --git a/webapp/utils/channel_utils.jsx b/webapp/utils/channel_utils.jsx
index 50573e077..ffc69d7b4 100644
--- a/webapp/utils/channel_utils.jsx
+++ b/webapp/utils/channel_utils.jsx
@@ -76,10 +76,21 @@ export function completeDirectChannelInfo(channel) {
     });
 }
 
+const defaultPrefix = 'D'; // fallback for future types
+const typeToPrefixMap = {[Constants.OPEN_CHANNEL]: 'A', [Constants.PRIVATE_CHANNEL]: 'B', [Constants.DM_CHANNEL]: 'C'};
+
 export function sortChannelsByDisplayName(a, b) {
     const locale = LocalizationStore.getLocale();
 
-    return buildDisplayNameAndTypeComparable(a).localeCompare(buildDisplayNameAndTypeComparable(b), locale, {numeric: true});
+    if (a.type !== b.type) {
+        return (typeToPrefixMap[a.type] || defaultPrefix).localeCompare((typeToPrefixMap[b.type] || defaultPrefix), locale);
+    }
+
+    if (a.display_name !== b.display_name) {
+        return a.display_name.localeCompare(b.display_name, locale, {numeric: true});
+    }
+
+    return a.name.localeCompare(b.name, locale, {numeric: true});
 }
 
 export function showCreateOption(channelType, isAdmin, isSystemAdmin) {
@@ -200,10 +211,3 @@ function not(f) {
 function andX(...fns) {
     return (...args) => fns.every((f) => f(...args));
 }
-
-const defaultPrefix = 'D'; // fallback for future types
-const typeToPrefixMap = {[Constants.OPEN_CHANNEL]: 'A', [Constants.PRIVATE_CHANNEL]: 'B', [Constants.DM_CHANNEL]: 'C'};
-
-function buildDisplayNameAndTypeComparable(channel) {
-    return (typeToPrefixMap[channel.type] || defaultPrefix) + channel.display_name + channel.name;
-}
-- 
cgit v1.2.3-1-g7c22

