title: Getting nearest previous date, sort without modifying original array
body: <p>I'm trying to get the nearest date to <code>formatted_date</code>=2020-08-04 in this way, where the nearest is the previous one --&gt; <code>2020-08-02</code></p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var arrayDateValue= [];
  var formattedDate = '2020-08-04';
  arrayDateValue.push({
          date: '2020-08-01',
          value: 1111     });
  arrayDateValue.push({
          date: '2020-08-02',
          value: 1212     });
  arrayDateValue.push({
          date: '2020-08-05',
          value: 1313     });
  arrayDateValue.push({
          date: '2020-08-06',
          value: 1313     });
  
  
  
  var prevIndexIfNotFound = arrayDateValue
  .sort((a, b) =&gt; (new Date(b.date) - new Date(a.date)))
  .findIndex(date =&gt; new Date(date.date) - new Date(formattedDate) &lt;= 0);
  
  console.log(arrayDateValue[prevIndexIfNotFound]);</code></pre>
  </div>
  </div>
  </p>
  <p>The result is perfect, but -</p>
  <ol>
  <li><p>I need <code>arrayDateValue</code> to further process, how to sort it without modifying it?</p>
  </li>
  <li><p>The processing time is a little bit slower when my array is big, is there efficient way to optimize such requests?</p>
  </li>
  </ol>
  <p>Thanks a lot!</p>