Skip to content

Commit

Permalink
UIBULKED-375: Localize alphabetical order of Bulk edit elements (#424)
Browse files Browse the repository at this point in the history
  • Loading branch information
UladzislauKutarkin authored Dec 8, 2023
1 parent 3884df6 commit dc8cdde
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
25 changes: 22 additions & 3 deletions src/utils/sortAlphabetically.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,30 @@
export const sortAlphabetically = (array, placeholder) => array.sort((a, b) => {
const collator = new Intl.Collator();

if (a.label === placeholder) {
return -1;
} else if (placeholder) {
} else if (b.label === placeholder) {
return 1;
} else {
return a.label.localeCompare(b.label);
}
// Compare based on category
const aCategory = a.categoryName || '';
const bCategory = b.categoryName || '';
const categoryComparison = collator.compare(aCategory, bCategory);

// If one item has no category, compare label to category name
if (!a.categoryName && b.categoryName) {
return collator.compare(a.label, bCategory);
} else if (a.categoryName && !b.categoryName) {
return collator.compare(aCategory, b.label);
}

// If same category, sort based on label with lower priority
if (categoryComparison === 0) {
return collator.compare(a.label, b.label);
}

// Prioritize sorting based on category
return categoryComparison;
});

export const sortAlphabeticallyActions = (array, placeholder) => {
Expand Down
5 changes: 3 additions & 2 deletions src/utils/sortAlphabetically.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ describe('sortAlphabetically', () => {

const expectedOutput = [
{ label: 'Placeholder' },
{ label: 'URL public note' },
{ label: 'Link text' },
{ label: 'Available' },
{ label: 'Link text' },
{ label: 'URL public note' },

];

const result = sortAlphabetically(inputArray, 'Placeholder');
Expand Down

0 comments on commit dc8cdde

Please sign in to comment.