I have a list of entries which I want to be sorted according to date at all times. So I incorporated a sorting method in a `useEffect` hook which is supposed to run after a new entry is added.

```
  useEffect(() => {
    setEntries(entries.sort((a, b) => {
      if (a.date > b.date) {
        return -1;
      }
      else if (a.date < b.date) {
        return -1
      }
      else {
        return 0;
      }
    }));
 }, [entries])

```

Since `entries` is in the dependency array, `useEFfect` should be triggered after a change in entries (entry added). When `useEffect` runs, `setEntries` is invoked, thus the state is changed and I believe it should re-render. Unfortunately this is not happening.

What is going wrong here?

Code for updating the list:

```
function addEntry(date, description, header, amount) {
    if (!date || !description || !header || amount < 0) {
      return;
    }
    setEntries(prev => ([
      {id: Date.now(), date: date, description: description, header: header, amount: amount}, ...prev
    ]))

    setDescription('');
    setHeader('');
    setAmount('');
   }

```

I tried to incorporate the sorting function here at first, but although this function is working, strange enough I cannot access the updated `entries` in the same function after it is updated.