Skip to content

Commit

Permalink
OCLOMRS-907: Sort Names Alphabetically by Language (#650)
Browse files Browse the repository at this point in the history
  • Loading branch information
jwnasambu committed Jan 12, 2021
1 parent f7f2227 commit b9dc395
Showing 1 changed file with 61 additions and 2 deletions.
63 changes: 61 additions & 2 deletions src/apps/concepts/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,12 @@ const apiConceptToConcept = (
let { names, descriptions, display_name, ...theRest } = apiConcept;
descriptions = descriptions || [];

let sortedNames = names.slice().sort(sortConceptName);
let sortedDescriptions = descriptions.slice().sort(sortConceptDescriptions);

return {
names: convertNames ? apiNamesToName(names) : names,
descriptions,
names: convertNames ? apiNamesToName(sortedNames) : sortedNames,
descriptions: sortedDescriptions,
...theRest,
answers: mappings.filter(
mapping => mapping.map_type === MAP_TYPE_Q_AND_A.value
Expand All @@ -117,6 +120,62 @@ const apiConceptToConcept = (
};
};

const sortConceptName = (n1: ConceptName, n2: ConceptName) => {
if (typeof n1 === 'undefined') {
return (typeof n2 === 'undefined') ? 0 : 1;
}

if (typeof n2 === 'undefined') {
return -1;
}

if (n1.locale !== n2.locale) {
return n1.locale < n2.locale ? -1 : 1;
}

if (n1.locale_preferred !== n2.locale_preferred) {
return n1.locale_preferred ? -1 : 1;
}

if (n1.name_type !== n2.name_type) {
if (n1.name_type === "FULLY_SPECIFIED") {
return -1;
} else if (n2.name_type === "FULLY_SPECIFIED") {
return 1;
}
}

if (n1.name !== n2.name) {
return n1.name < n2.name ? -1 : 1;
}

return 0;
}

const sortConceptDescriptions = (d1: ConceptDescription, d2: ConceptDescription) => {
if (typeof d1 === 'undefined') {
return (typeof d2 === 'undefined') ? 0 : 1;
}

if (typeof d2 === 'undefined') {
return -1;
}

if (d1.locale !== d2.locale) {
return d1.locale < d2.locale ? -1 : 1;
}

if (d1.locale_preferred !== d2.locale_preferred) {
return d1.locale_preferred ? -1 : 1;
}

if (d1.description !== d2.description) {
return d1.description < d2.description ? -1 : 1;
}

return 0;
}

export type SortableField =
| "bestMatch"
| "lastUpdate"
Expand Down

0 comments on commit b9dc395

Please sign in to comment.