Skip to content

Commit

Permalink
fix(naturalCompare): null values are properly sorted
Browse files Browse the repository at this point in the history
  • Loading branch information
mbarbeau committed Oct 17, 2019
1 parent 96afac9 commit eaa7565
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
3 changes: 2 additions & 1 deletion packages/common/src/lib/entity/shared/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,8 @@ export class EntityView<E extends object, V extends object = E> {
return ObjectUtils.naturalCompare(
clause.valueAccessor(v1),
clause.valueAccessor(v2),
clause.direction
clause.direction,
false
);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class FilterableDataSourcePipe implements PipeTransform {
dataSource.options.ogcFilters.editable
) {
isOgcFilterable = true;
}
}
if (
dataSource.options.ogcFilters &&
dataSource.options.ogcFilters.enabled &&
Expand Down
20 changes: 10 additions & 10 deletions packages/utils/src/lib/object-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,22 +99,22 @@ export class ObjectUtils {
return obj;
}

static naturalCompare(a, b, direction = 'asc', nullFirst = false) {
static naturalCompare(a, b, direction = 'asc', nullsFirst?: boolean) {
if (direction === 'desc') {
b = [a, (a = b)][0];
}

if (a === undefined || a === null || a === '') {
// nullsFirst = undefined : end if direction = 'asc', first if direction = 'dess'
// nullsFirst = true : always first
// nullsFirst = false : always end
// *** 'undefined' values are always at the end of array despite the 'nullsFirst' option
if (a === null || a === '' || b === null || b === '') {
const nullScore =
a === b ? 0 : a === null ? 2 : b === null ? -2 : a === '' ? 1 : -1;
if (direction === 'desc') {
return nullFirst ? -1 : 1;
return nullsFirst !== false ? nullScore : nullScore * -1;
}
return nullFirst ? 1 : -1;
}
if (b === undefined || b === null || b === '') {
if (direction === 'desc') {
return nullFirst ? 1 : -1;
}
return nullFirst ? -1 : 1;
return nullsFirst === true ? nullScore * -1 : nullScore;
}

const ax = [];
Expand Down

0 comments on commit eaa7565

Please sign in to comment.