From 7b1ccc22524273a2e0ba138bd3f5203f4ccb5e54 Mon Sep 17 00:00:00 2001 From: Vyacheslav Matyukhin Date: Sat, 7 May 2022 01:39:04 +0400 Subject: [PATCH] fix: stable options sorting --- src/web/questions/components/HistoryChart.tsx | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/web/questions/components/HistoryChart.tsx b/src/web/questions/components/HistoryChart.tsx index a5e0600..7c07829 100644 --- a/src/web/questions/components/HistoryChart.tsx +++ b/src/web/questions/components/HistoryChart.tsx @@ -81,7 +81,14 @@ const Legend: React.FC<{ const buildDataSets = (question: QuestionWithHistoryFragment) => { let dataSetsNames = question.options - .sort((a, b) => (a.probability > b.probability ? -1 : 1)) + .sort((a, b) => { + if (a.probability > b.probability) { + return -1; + } else if (a.probability < b.probability) { + return 1; + } + return a.name < b.name ? -1 : 1; // needed for stable sorting - otherwise it's possible to get order mismatch in SSR vs client-side + }) .map((o) => o.name) .slice(0, MAX_LINES);