Skip to content

Commit

Permalink
add sort utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Feb 15, 2024
1 parent cbb202c commit 0ec97aa
Showing 1 changed file with 15 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
limitations under the License.
-%>
import { Injectable } from '@angular/core';
import { ASC, DESC } from 'app/config/navigation.constants';
import { SortState } from './sort-state';

@Injectable({ providedIn: 'root' })
Expand All @@ -27,9 +26,15 @@ export class SortService {
sensitivity: 'base',
});

public startSort({ predicate, order }: Required<SortState>): (a: any, b: any) => number {
public startSort({ predicate, order }: Required<SortState>, fallback?: Required<SortState>): (a: any, b: any) => number {
const multiply = order === 'desc' ? -1 : 1;
return (a: any, b: any) => this.collator.compare(a[predicate], b[predicate]) * multiply;
return (a: any, b: any) => {
const compare = this.collator.compare(a[predicate], b[predicate]);
if (compare === 0 && fallback) {
return this.startSort(fallback)(a, b);
}
return compare * multiply;
};
}

public parseSortParam(sortParam: string | undefined): SortState {
Expand All @@ -40,9 +45,13 @@ export class SortService {
}
}
return { predicate: sortParam?.length ? sortParam : undefined };
};
}

public buildSortParam(sortState: SortState): string[] {
return sortState.predicate && sortState.order !== undefined ? [`${sortState.predicate},${sortState.order}`] : [];
public buildSortParam({ predicate, order }: SortState, fallback?: string): string[] {
const sortParam = predicate && order ? [`${predicate},${order}`] : [];
if (fallback && predicate !== fallback) {
sortParam.push(`${fallback},asc`);
}
return sortParam;
}
}

0 comments on commit 0ec97aa

Please sign in to comment.