Skip to content

Commit 112d5c6

Browse files
authored
Better sorting (#103)
1 parent cfcc4ca commit 112d5c6

File tree

1 file changed

+60
-16
lines changed

1 file changed

+60
-16
lines changed

src/components/members-list/members-list.component.tsx

Lines changed: 60 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -109,34 +109,78 @@ const MembersList = ({
109109
};
110110
})
111111
.sort((m1, m2) => {
112-
// Order present members by current session duration
112+
// Order online members on longest session duration (longest first)
113113
if (m1.office_times.is_active && m2.office_times.is_active) {
114114
return (
115115
m2.office_times.current_session_duration -
116116
m1.office_times.current_session_duration
117117
);
118118
}
119-
// Order present members before absent members
119+
// If only one online show the online member first
120120
if (m1.office_times.is_active !== m2.office_times.is_active) {
121121
return !m1.office_times.is_active ? 1 : -1;
122122
}
123-
// Order absent members by last seen date
124-
if (!m1.office_times.is_active && !m2.office_times.is_active) {
125-
if (m1.office_times.last_seen && m2.office_times.last_seen) {
126-
return m1.office_times.last_seen < m2.office_times.last_seen ? 1 : -1;
127-
}
128-
if (m1.office_times.last_seen) {
129-
return -1;
130-
}
131-
if (m2.office_times.last_seen) {
132-
return 1;
133-
}
123+
124+
// Members are offline:
125+
// a) Members are active (newest first)
126+
if (
127+
m1.is_active &&
128+
m2.is_active &&
129+
m1.office_times.last_seen &&
130+
m2.office_times.last_seen
131+
) {
132+
return m1.office_times.last_seen < m2.office_times.last_seen ? 1 : -1;
133+
}
134+
135+
// b) One of the members has a last_seen (last_seen first)
136+
if (
137+
m1.is_active &&
138+
m1.office_times.last_seen &&
139+
!m2.office_times.last_seen
140+
)
141+
return -1;
142+
if (
143+
m2.is_active &&
144+
m2.office_times.last_seen &&
145+
!m1.office_times.last_seen
146+
)
147+
return 1;
148+
149+
// 3. Active member without last_seen (before inactive)
150+
if (m1.is_active !== m2.is_active) {
151+
return m1.is_active ? -1 : 1;
134152
}
135-
// Order absent members with no last seen date by active status
153+
154+
// 5. If both is inactive (newest last_seen first)
155+
if (
156+
!m1.is_active &&
157+
!m2.is_active &&
158+
m1.office_times.last_seen &&
159+
m2.office_times.last_seen
160+
) {
161+
return m1.office_times.last_seen < m2.office_times.last_seen ? 1 : -1;
162+
}
163+
164+
// 6. One inactive with last_seen (one with last_seen first)
165+
if (
166+
!m1.is_active &&
167+
m1.office_times.last_seen &&
168+
!m2.office_times.last_seen
169+
)
170+
return -1;
171+
if (
172+
!m2.is_active &&
173+
m2.office_times.last_seen &&
174+
!m1.office_times.last_seen
175+
)
176+
return 1;
177+
178+
// 7. Active without last seen should be before inactives
136179
if (m1.is_active !== m2.is_active) {
137-
return m2.is_active ? 1 : -1;
180+
return m1.is_active ? -1 : 1;
138181
}
139-
// Lastly, order by name
182+
183+
// Alphabetic fallback
140184
return m1.name.localeCompare(m2.name);
141185
});
142186

0 commit comments

Comments
 (0)