Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort facet values if they are numbers #5865

Merged
merged 6 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/volto/news/5864.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Correctly sort facet values if they are numbers @erral
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ const defaultShowFacet = (index) => {
: values && Object.keys(values).length > 0;
};

export const sortFacetChoices = (choices) => {
const sorted_choices = choices.sort((a, b) =>
typeof a.label === 'string' && typeof b.label === 'string'
? a.label.localeCompare(b.label, 'en', { sensitivity: 'base' })
: typeof a.label === 'number' && typeof b.label == 'number'
? a.label - b.label
: 0,
);

return sorted_choices;
};

const Facets = (props) => {
const [hidden, setHidden] = useState(true);
const {
Expand Down Expand Up @@ -81,9 +93,7 @@ const Facets = (props) => {
: true,
);

choices = choices.sort((a, b) =>
a.label.localeCompare(b.label, 'en', { sensitivity: 'base' }),
);
choices = sortFacetChoices(choices);

const isMulti = facetSettings.multiple;
const selectedValue = facets[facetSettings?.field?.value];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { sortFacetChoices } from './Facets';

describe('sortFacetChoices', () => {
it('sort choices with string labels', () => {
const choices = [
{ label: 'b' },
{ label: 'd' },
{ label: 'a' },
{ label: 'c' },
];
const sortedChoices = sortFacetChoices(choices);
expect(sortedChoices).toStrictEqual([
{ label: 'a' },
{ label: 'b' },
{ label: 'c' },
{ label: 'd' },
]);
});
it('sort choices with string labels with accents (1)', () => {
const choices = [
{ label: 'éventa' },
{ label: 'portal' },
{ label: 'newsitem' },
{ label: 'eventb' },
];
const sortedChoices = sortFacetChoices(choices);
expect(sortedChoices).toStrictEqual([
{ label: 'éventa' },
{ label: 'eventb' },
{ label: 'newsitem' },
{ label: 'portal' },
]);
});

it('sort choices with string labels with accents (2)', () => {
const choices = [
{ label: 'eventa' },
{ label: 'portal' },
{ label: 'newsitem' },
{ label: 'éventb' },
];
const sortedChoices = sortFacetChoices(choices);
expect(sortedChoices).toStrictEqual([
{ label: 'eventa' },
{ label: 'éventb' },
{ label: 'newsitem' },
{ label: 'portal' },
]);
});

it('sort choices with int labels', () => {
const choices = [{ label: 7 }, { label: 3 }, { label: 1 }, { label: 4 }];
const sortedChoices = sortFacetChoices(choices);
expect(sortedChoices).toStrictEqual([
{ label: 1 },
{ label: 3 },
{ label: 4 },
{ label: 7 },
]);
});
it('sort choices with labels of any kind', () => {
const choices = [
{ label: 7 },
{ label: '1' },
{ label: 'b' },
{ label: 5 },
];
const sortedChoices = sortFacetChoices(choices);
expect(sortedChoices).toStrictEqual([
{ label: 7 },
{ label: '1' },
{ label: 'b' },
{ label: 5 },
]);
});
});
Loading