import React from 'react'
import { type Meta } from '@storybook/react-vite'
import { sortByProperty } from '../../services/HelperServiceTyped'
import { DocsTemplate } from '../../../.storybook'
export const Example = (args: {
/** The data array to sort */
data: object[]
/** The property name to sort by */
property: string
/** If true, sorts in descending order. If false, sorts in ascending order. */
isDescending?: boolean
}): React.JSX.Element => {
const { data, property, isDescending } = args
const sortedData = sortByProperty(
[...data],
property as keyof (typeof data)[0],
isDescending,
)
return (
<>
Sorted Data:
{JSON.stringify(sortedData, null, 2)}
>
)
}
Example.storyName = 'sortByProperty'
Example.args = {
data: [
{ id: 1, name: 'Alpha', value: 42, isActive: true },
{ id: 2, name: 'Beta', value: null, isActive: false },
{ id: 3, name: 'Gamma', value: 15, isActive: true },
{ id: 4, name: 'Delta', value: 78, isActive: false },
{ id: 5, name: 'Epsilon', value: 33, isActive: true },
],
property: 'name',
isDescending: false,
}
export default {
title: 'Helper Functions/sortByProperty',
component: Example,
argTypes: {
data: {
control: 'object',
description: 'Array of objects to sort',
},
property: {
control: 'text',
description: 'Property to sort by',
},
isDescending: {
control: 'boolean',
description: 'Sort in descending order',
},
},
parameters: {
viewMode: 'docs',
previewTabs: {
canvas: { hidden: true },
},
docs: {
page: () => (
<>
sortByProperty is used to sort arrays of objects by
a specific property.
,
The function takes three arguments:
- The array of objects to sort
- The property name to sort by
-
A boolean flag:
true for descending order,{' '}
false for ascending
,
Note: The function handles null values by pushing them to the
end of the sorted array.
,
]}
/>
>
),
},
},
} as Meta<{
data: object[]
property: string
isDescending: boolean
}>