title: Array sort with React.useState breaks when adding certain date
body: <p>I'm trying to add a date in a list and sort that list to ascending every time I add a new date. Everything is fine, the list is being resorted when every new data is inserted, but if I start to add this date <code>13/02/22</code> the re-sorting will seem to have stopped and the new date inserts will be will just stack to each other/latest insertion will be the last regardless of its actual date.</p>
  <p><a href="https://i.stack.imgur.com/aPKlp.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/aPKlp.png" alt="enter image description here" /></a></p>
  <p>Here is my code when adding a date to the list. And the <code>useEffect</code> in here is my sorting logic. As you can see I sort my list of dates by calling the <code>setAddedOpens</code> which is a React.useState.</p>
  <pre><code>const [addedOpens, setAddedOpens] = React.useState(defaultOpens);
   const [toggleSort, setToggleSort] = React.useState(false);
  
        const _addOpens = () =&gt; {
          for (let week = 0; week &lt;= repeatWeeks; week++) {
            const OFI = new Date(getValues('OFIdate'));
      
            setAddedOpens((prevOpens) =&gt; [
              ...prevOpens,
              {
                ofi: format(OFI.setDate(OFI.getDate() + 7 * week), 'dd/MM/yyyy'),
                start: format(getValues('startTime'), 'hh:mm a'),
                end: format(getValues('endTime'), 'hh:mm a')
              }
            ]);
          }
      
          setToggleSort((toggle) =&gt; !toggle);
        };
      
        React.useEffect(() =&gt; {
          setAddedOpens([...addedOpens].sort((a, b) =&gt; new Date(a.ofi) - new Date(b.ofi)));
        }, [toggleSort]);
  </code></pre>
  <p>Don't mind the other parts of my code here. Because I also have a feature you can bulk add dates. But in the meantime let's say I'm just adding a single date every time I click ADD and that<code>repeatWeeks</code> state is always 0.</p>
  <p>I'm not so sure what could be the problem is. Maybe I'm missing something in my logic? Or this has something to do with dates or date formatting? Why is the sorting getting erroneous once I added the date <code>13/02/22</code> ?</p>
  <p>Thanks in advance.</p>