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 #3023: Sort nulls like Excel by default at bottom #3026

Merged
merged 1 commit into from
Jul 4, 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
11 changes: 4 additions & 7 deletions components/lib/datatable/DataTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -861,8 +861,8 @@ export const DataTable = React.forwardRef((props, ref) => {
return props.removableSort ? (props.defaultSortOrder === currentOrder ? currentOrder * -1 : 0) : currentOrder * -1;
}

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

const addSortMeta = (meta, multiSortMeta) => {
Expand Down Expand Up @@ -904,9 +904,8 @@ export const DataTable = React.forwardRef((props, ref) => {
value.sort((data1, data2) => {
const value1 = ObjectUtils.resolveFieldData(data1, field);
const value2 = ObjectUtils.resolveFieldData(data2, field);
const result = compareValuesOnSort(value1, value2);

return (order * result);
return compareValuesOnSort(value1, value2, order);;
});
}

Expand Down Expand Up @@ -953,9 +952,7 @@ export const DataTable = React.forwardRef((props, ref) => {
return (multiSortMeta.length - 1) > (index) ? (multisortField(data1, data2, multiSortMeta, index + 1)) : 0;
}

const result = compareValuesOnSort(value1, value2);

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

const onFilterChange = (filters) => {
Expand Down
13 changes: 8 additions & 5 deletions components/lib/utils/ObjectUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,15 @@ export default class ObjectUtils {
static sort(value1, value2, order = 1, locale) {
let result = null;

if (value1 == null && value2 != null)
result = -1;
else if (value1 != null && value2 == null)
result = 1;
else if (value1 == null && value2 == null)
const emptyValue1 = this.isEmpty(value1);
const emptyValue2 = this.isEmpty(value2);

if (emptyValue1 && emptyValue2)
result = 0;
else if (emptyValue1)
result = order; // sort nulls at bottom like Excel
else if (emptyValue2)
result = -order; // sort nulls at bottom like Excel
else if (typeof value1 === 'string' && typeof value2 === 'string')
result = value1.localeCompare(value2, locale, { numeric: true });
else
Expand Down