I have an array of objects and I should sorting this array, but I have a problem because my sorting consider the capital letter before others (for example “Boat” results first of “apple” ).
I need to show these data in a table, and I click the sorting criteria clicking on the table’s title that will set paginationState.sort
So I have thought to this solution, but it not sort the capital letter in the right way:
const [listSorting, setListSorting] = useState([])
useEffect(() => {
if (applicationList && applicationList > 0) {
sortingList();
}
}, [props.applicationList]);
const sortingList = () => {
const list = [...applicationList].sort((a, b) => {
if (typeof a[paginationState.sort] === 'string') {
if (paginationState.order === 'asc') a[paginationState.sort].toLowerCase().localeCompare(b[paginationState.sort].toLowerCase());
if (paginationState.order === 'desc') a[paginationState.sort].toLowerCase().localeCompare(b[paginationState.sort].toLowerCase());
} else if (typeof a[paginationState.sort] === 'number') {
if (paginationState.order === 'asc' && a[paginationState.sort] < b[paginationState.sort]) return -1;
if (paginationState.order === 'desc' && a[paginationState.sort] > b[paginationState.sort]) return 1;
}
return 0;
});
setListSorting(list);
};
So in the return I have :
<div className="table-responsive">
{listSorting && listSorting .length > 0 ? (
//......
<tbody>
{listSorting.map((application, i) => (
<tr key={`entity-${i}`} data-cy="entityTable">
My array of objects applicationList is like :
applicationList : [
{
appCod: 1,
appDescription: 'string'
appName: 'string'
},
{
......
},
]
So if click on table’s title appCod the value of paginationState.sort will be equal to ‘appCod’.
What would to obtain:
I have a problem with words that starts with capital letter because they are considered before others.
So I would like to get a sort that allows me to sort them correctly. How could I do?