From 986e89c344963c54b2cbed06d95256666c32b713 Mon Sep 17 00:00:00 2001 From: Daniel Palafox Date: Wed, 18 Oct 2023 22:39:27 -0500 Subject: [PATCH] fix: Update sort by source functionality to sort also alphabetically --- .../record-affiliations-sort.service.ts | 34 +++++++++++-------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/src/app/core/record-affiliations-sort/record-affiliations-sort.service.ts b/src/app/core/record-affiliations-sort/record-affiliations-sort.service.ts index d0efe278ed..f3ae922fc7 100644 --- a/src/app/core/record-affiliations-sort/record-affiliations-sort.service.ts +++ b/src/app/core/record-affiliations-sort/record-affiliations-sort.service.ts @@ -1,10 +1,6 @@ import { Injectable } from '@angular/core' import { MonthDayYearDate } from 'src/app/types' -import { - AffiliationUIGroup, - AffiliationGroup, - AffiliationType, -} from 'src/app/types/record-affiliation.endpoint' +import { AffiliationGroup, AffiliationType, AffiliationUIGroup } from 'src/app/types/record-affiliation.endpoint' import { UserRecordOptions } from 'src/app/types/record.local' @Injectable({ @@ -88,15 +84,9 @@ export class AffiliationsSortService { } if (by === 'source') { - affiliationGroup.sort((a, b) => { - return ( - Number(AffiliationsSortService.isSelfAsserted(a, orcid)) - - Number(AffiliationsSortService.isSelfAsserted(b, orcid)) - ) - }) - if (ascending) { - affiliationGroup.reverse() - } + const selfAsserted = this.getSelfAssertedOrValidatedAffiliations(affiliationGroup, ascending, orcid, 'self-asserted') + const validated = this.getSelfAssertedOrValidatedAffiliations(affiliationGroup, ascending, orcid, 'validated') + x.affiliationGroup = ascending ? [...selfAsserted, ...validated] : [...validated, ...selfAsserted] } } }) @@ -206,7 +196,21 @@ export class AffiliationsSortService { }) } - private static isSelfAsserted( + private getSelfAssertedOrValidatedAffiliations(affiliationGroup: AffiliationGroup[], ascending: boolean, orcid: string, type: 'self-asserted' | 'validated'): AffiliationGroup[] { + return affiliationGroup + .filter( + (affiliationGroup) => { + const selfAsserted = this.isSelfAsserted(affiliationGroup, orcid) + return type === 'self-asserted' ? selfAsserted : !selfAsserted + }, + ).sort((a, b) => { + return ( + '' + a.defaultAffiliation.affiliationName.value + ).localeCompare('' + b.defaultAffiliation.affiliationName.value) + }) + } + + private isSelfAsserted( affiliationGroup: AffiliationGroup, orcid: string ): boolean {