From c26bdc5ffb0fb651785e00412574c2aba4d013f8 Mon Sep 17 00:00:00 2001 From: Eric Eastwood Date: Fri, 2 Jun 2023 17:19:58 -0500 Subject: [PATCH] Fix Firefox sorting room cards in the wrong direction (#261) Room cards will now sort by room members descending (highest to lowest) as expected. Fix https://github.com/matrix-org/matrix-public-archive/issues/218 The `/publicRooms` (room directory) endpoint already returns rooms in the correct order which is why we didn't care about the order before but the different `[].sort(...)` implementations in browsers necessitates we be explicit about it. Ideally, we wouldn't have to use the `ObservableMap.sortValues()` method at all but it seems like one of the only ways to get the values out. In any case, maybe it's more clear what order things are in now. This bug stems from the fact that `[1, 2, 3, 4, 5].sort((a, b) => 1)` returns different results in Chrome vs Firefox (found from https://stackoverflow.com/questions/55039157/array-sort-behaves-differently-in-firefox-and-chrome-edge) - Chrome: `[1, 2, 3, 4, 5].sort((a, b) => 1)` -> `[1, 2, 3, 4, 5]` :white_check_mark: - Firefox: `[1, 2, 3, 4, 5].sort((a, b) => 1)` -> `[5, 4, 3, 2, 1]` :x: --- shared/viewmodels/RoomDirectoryViewModel.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/shared/viewmodels/RoomDirectoryViewModel.js b/shared/viewmodels/RoomDirectoryViewModel.js index 1b28022..0014ac1 100644 --- a/shared/viewmodels/RoomDirectoryViewModel.js +++ b/shared/viewmodels/RoomDirectoryViewModel.js @@ -88,9 +88,15 @@ class RoomDirectoryViewModel extends ViewModel { ); }); this._roomCardViewModelsFilterMap = new ApplyMap(this._roomCardViewModelsMap); - this._roomCardViewModels = this._roomCardViewModelsFilterMap.sortValues((/*a, b*/) => { - // Sort doesn't matter - return 1; + this._roomCardViewModels = this._roomCardViewModelsFilterMap.sortValues((a, b) => { + // Sort by the number of joined members descending (highest to lowest) + if (b.numJoinedMembers > a.numJoinedMembers) { + return 1; + } else if (b.numJoinedMembers < a.numJoinedMembers) { + return -1; + } + + return 0; }); this._safeSearchEnabled = true;