'How can I show the latest data by date using sort in react typescript

My date format is like this: Mon Mar 07 2022 and I want to show the latest data. I have tried this way but it's not working.

If I put new Date() into my code I got this error: var Date: DateConstructor new (value: string | number | Date) => Date (+3 overloads) The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.

 useEffect(() => {
        setIsLoading(true)
        fetch(`http://localhost:5000/dashboard/orders`)
            .then(res => res.json())
            .then(data => {
                const latestData = data.sort((a, b) => b.date - a.date)
// const latestData = data.sort((a, b) => new Date(b.date) -new Date( a.date)) // error
                setOrders(latestData)
            })
            .finally(() => setIsLoading(false))
    }, [])


Solution 1:[1]

Please Try this way

data.sort(function(a,b){
  return new Date(b.date) - new Date(a.date);
});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Vida Hedayati