diff --git a/components/lib/datatable/DataTable.js b/components/lib/datatable/DataTable.js index 4e25ad0e1a..9e31554e17 100644 --- a/components/lib/datatable/DataTable.js +++ b/components/lib/datatable/DataTable.js @@ -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; } diff --git a/components/lib/treetable/TreeTable.js b/components/lib/treetable/TreeTable.js index 00c34a4ce2..0c00ef5163 100644 --- a/components/lib/treetable/TreeTable.js +++ b/components/lib/treetable/TreeTable.js @@ -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++) { @@ -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) => { diff --git a/components/lib/utils/ObjectUtils.js b/components/lib/utils/ObjectUtils.js index c99314c28d..fea67b3a9b 100644 --- a/components/lib/utils/ObjectUtils.js +++ b/components/lib/utils/ObjectUtils.js @@ -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); @@ -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; } }