-2

I take threads from the backend endpoint

type Thread = {
    threadId: string,
    title: string;
    author: string;
    lastReply: string;
    replyTime: string;
    creationTime: string;
}

const HomePage: React.FC = () => {
    const loggedIn = authHeader();
    const classes = useStyles();
    const {
        threads,
        categories,
    } = useMainData();

 return (
        <>
...
...

I want to sort threads by creationTime and pass it to the component in that order. Or maybe it's better to pass just threads and sort it into the component? I was thinking about sth like this:

const Categories: React.FC<ThreadsListProps> = (props) => {
    const {
        latestthreads
    } = props;
    const sortedthreads = latestthreads.sort((a, b) => b.creationTime - a.creationTime);

    const classes = useStyles();

    return (...

but time creation is string

Rebai Ahmed
  • 1,025
  • 1
  • 10
  • 18
ever
  • 1
  • 1
  • 1
    What is the format of `creationTime` string? – spender May 19 '21 at 12:31
  • Have you tried using the `.sort()` method on your array? – Jeremy Thille May 19 '21 at 12:33
  • if `threads` is an `Thread[]` you can use the [`Array.prototype.sort()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort) function. If your creationTime has a proper format (for instance ISO) it will work out of the box. Otherwise, you'll have to specify a compare funciton – derpirscher May 19 '21 at 12:33

0 Answers0