Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix: sort table decimals without floating-point loss

Afloat949542c9974d
diff --git a/src/lib/table-data.ts b/src/lib/table-data.ts
index 47bcb3babf..87c1113374 100644
--- a/src/lib/table-data.ts
+++ b/src/lib/table-data.ts
@@ -39,19 +39,37 @@ const AMOUNT = /^[+-]?[$€£¥₹]?\d{1,3}(?:,?\d{3})*(?:\.\d+)?%?$/;
/** A cell that reads as a quantity (a plain number, a price, a percentage), so its column lines up on the right. */
export function isTableAmount(text: string): boolean { return isTableNumber(text) || AMOUNT.test(text.trim()); }
+function decimalParts(text: string): { sign: number; order: bigint; digits: string } {
+ const negative = text.startsWith("-");
+ const [mantissa = "", exponent = "0"] = text.replace(/^[+-]/, "").toLowerCase().split("e");
+ const dot = mantissa.indexOf(".");
+ const decimals = dot < 0 ? 0 : mantissa.length - dot - 1;
+ const digits = mantissa.replace(".", "").replace(/^0+/, "");
+ if (!digits) return { sign: 0, order: 0n, digits: "" };
+ return { sign: negative ? -1 : 1, order: BigInt(exponent) + BigInt(digits.length - decimals), digits: digits.replace(/0+$/, "") };
+}
+
+/** Different floating-point values keep decimal order; equal rounded values need their original decimal digits. */
+function compareTableNumbers(left: string, right: string): number {
+ const l = Number(left), r = Number(right);
+ if (l < r) return -1;
+ if (l > r) return 1;
+ const a = decimalParts(left.trim()), b = decimalParts(right.trim());
+ if (a.sign !== b.sign) return a.sign - b.sign;
+ if (!a.sign) return 0;
+ const magnitude = a.order < b.order ? -1 : a.order > b.order ? 1
+ : a.digits < b.digits ? -1 : a.digits > b.digits ? 1 : 0;
+ return a.sign * magnitude;
+}
+
export function tableRowOrder(rows: readonly (readonly string[])[], query: string, sort: TableSort): number[] {
const needle = query.trim().toLocaleLowerCase();
const order = rows.flatMap((row, index) => !needle || row.some((cell) => cell.toLocaleLowerCase().includes(needle)) ? [index] : []);
if (sort) order.sort((a, b) => {
const left = rows[a]![sort.column] ?? "", right = rows[b]![sort.column] ?? "";
if (!left.trim() || !right.trim()) return Number(!left.trim()) - Number(!right.trim()) || a - b;
- const numeric = isTableNumber(left) && isTableNumber(right);
- let difference: number;
- if (numeric && /^[+-]?\d+$/.test(left.trim()) && /^[+-]?\d+$/.test(right.trim())) {
- const l = BigInt(left.trim()), r = BigInt(right.trim());
- difference = l < r ? -1 : l > r ? 1 : 0;
- } else difference = numeric ? Number(left) - Number(right) : collator.compare(left, right);
- return (Number.isFinite(difference) ? difference : collator.compare(left, right)) * (sort.descending ? -1 : 1) || a - b;
+ const difference = isTableNumber(left) && isTableNumber(right) ? compareTableNumbers(left, right) : collator.compare(left, right);
+ return difference * (sort.descending ? -1 : 1) || a - b;
});
return order;
}
diff --git a/src/lib/table-decimal-order.test.ts b/src/lib/table-decimal-order.test.ts
new file mode 100644
index 0000000000..26aeb1a12c
--- /dev/null
+++ b/src/lib/table-decimal-order.test.ts
@@ -0,0 +1,37 @@
+import { describe, expect, it } from "vitest";
+
+import { tableRowOrder } from "./table-data";
+
+describe("numeric table order without floating-point loss", () => {
+ it.each([
+ ["9007199254740992.2", "9007199254740992.1"],
+ ["1.00000000000000000002", "1.00000000000000000001"],
+ ["-1.00000000000000000001", "-1.00000000000000000002"],
+ ["1e309", "2e308"],
+ ["-2e308", "-1e309"],
+ ["2e-400", "1e-400"],
+ ["-1e-400", "-2e-400"],
+ ["1e9007199254740993", "1e9007199254740992"],
+ ])("puts %s after %s in ascending order", (larger, smaller) => {
+ const rows = [[larger], [smaller], [""], [" "]];
+ expect(tableRowOrder(rows, "", { column: 0, descending: false })).toEqual([1, 0, 2, 3]);
+ expect(tableRowOrder(rows, "", { column: 0, descending: true })).toEqual([0, 1, 2, 3]);
+ });
+
+ it("keeps equal values in source order across decimal and exponent spellings", () => {
+ const rows = [["1.20"], ["12e-1"], ["+001.2000"], [".12e1"], ["1.2"]];
+ for (const descending of [false, true]) {
+ expect(tableRowOrder(rows, "", { column: 0, descending })).toEqual([0, 1, 2, 3, 4]);
+ }
+ });
+
+ it("orders signed zero between tiny negative and positive values", () => {
+ const rows = [["-0"], ["0e999999999999999999999"], ["1e-9999"], ["-1e-9999"], ["+0.0"]];
+ expect(tableRowOrder(rows, "", { column: 0, descending: false })).toEqual([3, 0, 1, 4, 2]);
+ });
+
+ it("compares matching decimal magnitudes without expanding their exponents", () => {
+ const rows = [["120001e9999"], ["12e10003"], ["123e10002"], ["12299e10000"]];
+ expect(tableRowOrder(rows, "", { column: 0, descending: false })).toEqual([1, 0, 3, 2]);
+ });
+});