diff --git a/webapp/src/app/modules/person-list/person-list.component.ts b/webapp/src/app/modules/person-list/person-list.component.ts index f78ef450..38469f7a 100644 --- a/webapp/src/app/modules/person-list/person-list.component.ts +++ b/webapp/src/app/modules/person-list/person-list.component.ts @@ -43,7 +43,7 @@ export class PersonListComponent implements OnInit{ this.personsService.searchPersonsByName(author_name).subscribe({ next: (result) => { - this.state.persons.set(result.persons ?? []); + this.state.persons.set(this.sortByRanking(result.persons ?? [])); this.state.hasSearched.set(true); this.isPersonsLoading = false }, @@ -72,6 +72,57 @@ export class PersonListComponent implements OnInit{ this.router.navigate(['/persons',id]); } + sortByRanking(persons: Person[]): Person[] { + const personsByRank = new Map(); + + for (const person of persons) { + const rank = this.getSortRank(person); + if (!personsByRank.has(rank)) { + personsByRank.set(rank, []); + } + personsByRank.get(rank)!.push(person); + } + + const sortedRanks = Array.from(personsByRank.keys()).sort((a, b) => a - b); + const sortedPersons: Person[] = []; + + for (const rank of sortedRanks) { + sortedPersons.push(...personsByRank.get(rank)!); + } + + return sortedPersons; + } + + getSortRank(person: Person): number { + if (this.hasInstitution(person) && this.hasOrcid(person)) { + return 0; + } + if (this.hasInstitution(person)) { + return 1; + } + if (this.hasOrcid(person)) { + return 2; + } + return 3; + } + + hasInstitution(person: Person): boolean { + if (!person.affiliations) { + return false; + } + for (const affiliation of person.affiliations) { + if (affiliation.institution) { + return true; + } + } + return false; + } + + hasOrcid(person: Person): boolean { + const ids = person.ids as { orcid?: string } | null | undefined; + return !!ids?.orcid; + } + // if a person has an ORCID, use it as the distinguishing ID, otherwise use the regular ID getDistinguishingId(person: Person): string { const ids = person.ids as { orcid?: string } | null | undefined;