Skip to content

Commit 82ddf50

Browse files
committed
[FIX] pivot: month granularity sorting
The `month` granularity sorting wasn't working. The issue was that we were using `Number(value)` instead of `toNumber(value)` to convert the field value to a number. closes #6335 Task: 4781810 Signed-off-by: Lucas Lefèvre (lul) <lul@odoo.com>
1 parent 534c5f4 commit 82ddf50

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/helpers/pivot/spreadsheet_pivot/data_entry_spreadsheet_pivot.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { CellValue, EvaluatedCell } from "../../../types";
1+
import { toNumber } from "../../../functions/helpers";
2+
import { CellValue, DEFAULT_LOCALE, EvaluatedCell } from "../../../types";
23
import { PivotDimension, PivotTableColumn, PivotTableRow } from "../../../types/pivot";
34
import { SpreadsheetPivotTable } from "../table_spreadsheet_pivot";
45
import { SpreadsheetPivotRuntimeDefinition } from "./runtime_definition_spreadsheet_pivot";
@@ -254,7 +255,9 @@ function compareDimensionValues(dimension: PivotDimension, a: string, b: string)
254255
return dimension.order === "asc" ? -1 : 1;
255256
}
256257
if (dimension.type === "integer" || dimension.type === "date") {
257-
return dimension.order === "asc" ? Number(a) - Number(b) : Number(b) - Number(a);
258+
return dimension.order === "asc"
259+
? toNumber(a, DEFAULT_LOCALE) - toNumber(b, DEFAULT_LOCALE)
260+
: toNumber(b, DEFAULT_LOCALE) - toNumber(a, DEFAULT_LOCALE);
258261
}
259262
return dimension.order === "asc" ? a.localeCompare(b) : b.localeCompare(a);
260263
}

tests/pivots/spreadsheet_pivot/spreadsheet_pivot.test.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,14 +535,23 @@ describe("Spreadsheet Pivot", () => {
535535
expect(getEvaluatedGrid(model, "B26:F26")).toEqual([["5", "9", "14", "Total", ""]]);
536536
});
537537

538-
test("month should be supported", () => {
538+
test("month should be supported and correctly ordered", () => {
539539
const model = createModelWithPivot("A1:I5");
540540
updatePivot(model, "1", {
541541
columns: [{ name: "Created on", granularity: "month", order: "asc" }],
542542
rows: [],
543543
measures: [{ name: "Expected Revenue", aggregator: "sum" }],
544544
});
545545
setCellContent(model, "A26", `=pivot(1)`);
546+
expect(getEvaluatedGrid(model, "B26:F26")).toEqual([
547+
["February 2024", "March 2024", "April 2024", "Total", ""],
548+
]);
549+
550+
updatePivot(model, "1", {
551+
columns: [{ name: "Created on", granularity: "month", order: "desc" }],
552+
rows: [],
553+
measures: [{ name: "Expected Revenue", aggregator: "sum" }],
554+
});
546555
expect(getEvaluatedGrid(model, "B26:F26")).toEqual([
547556
["April 2024", "March 2024", "February 2024", "Total", ""],
548557
]);

0 commit comments

Comments
 (0)