Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #3284: Multiple sorting with dates was not correct #3286

Merged
merged 1 commit into from
Sep 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion components/lib/datatable/DataTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -932,7 +932,8 @@ export const DataTable = React.forwardRef((props, ref) => {
const value1 = ObjectUtils.resolveFieldData(data1, multiSortMeta[index].field);
const value2 = ObjectUtils.resolveFieldData(data2, multiSortMeta[index].field);

if (value1 === value2) {
// check if they are equal handling dates and locales
if (ObjectUtils.compare(value1, value2, PrimeReact.locale) === 0) {
return multiSortMeta.length - 1 > index ? multisortField(data1, data2, multiSortMeta, index + 1) : 0;
}

Expand Down
24 changes: 10 additions & 14 deletions components/lib/treetable/TreeTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ export const TreeTable = React.forwardRef((props, ref) => {
const sortField = getSortField();
const value1 = ObjectUtils.resolveFieldData(node1.data, sortField);
const value2 = ObjectUtils.resolveFieldData(node2.data, sortField);
return ObjectUtils.sort(value1, value2, getSortOrder(), PrimeReact.locale, PrimeReact.nullSortOrder);
return compareValuesOnSort(value1, value2, getSortOrder());
});

for (let i = 0; i < value.length; i++) {
Expand Down Expand Up @@ -216,21 +216,17 @@ export const TreeTable = React.forwardRef((props, ref) => {
const multisortField = (node1, node2, multiSortMeta, index) => {
const value1 = ObjectUtils.resolveFieldData(node1.data, multiSortMeta[index].field);
const value2 = ObjectUtils.resolveFieldData(node2.data, multiSortMeta[index].field);
let result = null;

if (value1 == null && value2 != null) result = -1;
else if (value1 != null && value2 == null) result = 1;
else if (value1 == null && value2 == null) result = 0;
else {
if (value1 === value2) {
return multiSortMeta.length - 1 > index ? multisortField(node1, node2, multiSortMeta, index + 1) : 0;
} else {
if ((typeof value1 === 'string' || value1 instanceof String) && (typeof value2 === 'string' || value2 instanceof String)) return multiSortMeta[index].order * value1.localeCompare(value2, PrimeReact.locale, { numeric: true });
else result = value1 < value2 ? -1 : 1;
}

// check if they are equal handling dates and locales
if (ObjectUtils.compare(value1, value2, PrimeReact.locale) === 0) {
return multiSortMeta.length - 1 > index ? multisortField(node1, node2, multiSortMeta, index + 1) : 0;
}

return multiSortMeta[index].order * result;
return compareValuesOnSort(value1, value2, multiSortMeta[index].order);
};

const compareValuesOnSort = (value1, value2, order) => {
return ObjectUtils.sort(value1, value2, order, PrimeReact.locale, PrimeReact.nullSortOrder);
};

const filter = (value, field, mode) => {
Expand Down
12 changes: 8 additions & 4 deletions components/lib/utils/ObjectUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,14 @@ export default class ObjectUtils {
}

static sort(value1, value2, order = 1, locale, nullSortOrder = 1) {
let result = null;
const result = ObjectUtils.compare(value1, value2, locale, order);
// nullSortOrder == 1 means Excel like sort nulls at bottom
const finalSortOrder = nullSortOrder === 1 ? order : nullSortOrder;
return finalSortOrder * result;
}

static compare(value1, value2, locale, order = 1) {
let result = -1;
const emptyValue1 = this.isEmpty(value1);
const emptyValue2 = this.isEmpty(value2);

Expand All @@ -191,8 +197,6 @@ export default class ObjectUtils {
else if (typeof value1 === 'string' && typeof value2 === 'string') result = value1.localeCompare(value2, locale, { numeric: true });
else result = value1 < value2 ? -1 : value1 > value2 ? 1 : 0;

// nullSortOrder == 1 means Excel like sort nulls at bottom
const finalSortOrder = nullSortOrder === 1 ? order : nullSortOrder;
return finalSortOrder * result;
return result;
}
}