Browse Source

fix number sort problem if the score has too many fraction numbers (#447)

Signed-off-by: ryjiang <jiangruiyi@gmail.com>
ryjiang 1 year ago
parent
commit
d331027c16
1 changed files with 9 additions and 3 deletions
  1. 9 3
      client/src/utils/Sort.ts

+ 9 - 3
client/src/utils/Sort.ts

@@ -5,12 +5,18 @@ type numberObj = {
 export const descendingComparator = (
 export const descendingComparator = (
   a: numberObj,
   a: numberObj,
   b: numberObj,
   b: numberObj,
-  orderBy: React.ReactText
+  orderBy: string
 ) => {
 ) => {
-  if (b[orderBy] < a[orderBy]) {
+  // need to round the value to avoid floating point error
+  const roundValue = (value: number) => Math.round(value * 1e12) / 1e12;
+
+  const aValue = roundValue(a[orderBy]);
+  const bValue = roundValue(b[orderBy]);
+
+  if (bValue < aValue) {
     return -1;
     return -1;
   }
   }
-  if (b[orderBy] > a[orderBy]) {
+  if (bValue > aValue) {
     return 1;
     return 1;
   }
   }
   return 0;
   return 0;