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 #2157: Datatable multi-sorting not handling NULL values #2440

Merged
merged 1 commit into from
Dec 10, 2021
Merged
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
20 changes: 11 additions & 9 deletions src/components/datatable/DataTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -1181,19 +1181,21 @@ export class DataTable extends Component {
const value2 = ObjectUtils.resolveFieldData(data2, multiSortMeta[index].field);
let result = null;

if (typeof value1 === 'string' || value1 instanceof String) {
if (value1.localeCompare && (value1 !== value2)) {
return (multiSortMeta[index].order * value1.localeCompare(value2, undefined, { numeric: true }));
}
}
else {
result = (value1 < value2) ? -1 : 1;
}

if (value1 === value2) {
return (multiSortMeta.length - 1) > (index) ? (this.multisortField(data1, data2, multiSortMeta, index + 1)) : 0;
}

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 (typeof value1 === 'string' && typeof value2 === 'string')
result = value1.localeCompare(value2, undefined, { numeric: true });
else
result = (value1 < value2) ? -1 : (value1 > value2) ? 1 : 0;

return (multiSortMeta[index].order * result);
}

Expand Down