title: Sort a json array, first by search text and remaining results alphabetically
body: <p>I have a json array, which I need to short first based on some search text, and then alphabetically. so basically in below array if I search for <strong>&quot;beau&quot;</strong> everything starting with beau should be kept on top(sorted alphabetically), and the remaining results will be sorted also alphabetically(even if we have that search text somewhere in the middle of the text) and should start right after the ones</p>
  <p><strong>Need solution in Javascript</strong></p>
  <p>Array :</p>
  <pre><code>[
  {
   &quot;value&quot;: &quot;1&quot;,
   &quot;text&quot;: &quot;BEAUMONT Habitation 54&quot;
  },
  {
   &quot;value&quot;: &quot;2&quot;,
   &quot;text&quot;: &quot;BEAUMONT Place de Renival&quot;
  },
  {
   &quot;value&quot;: &quot;3&quot;,
   &quot;text&quot;: &quot;BEAUMONT Rue des Tiennes&quot;
  },
  {
   &quot;value&quot;: &quot;4&quot;,
   &quot;text&quot;: &quot;BEAUMONT Rue Grand Chemin&quot;
  },
  {
   &quot;value&quot;: &quot;5&quot;,
   &quot;text&quot;: &quot;BRUYERES Chênes&quot;
  },
  {
   &quot;value&quot;: &quot;6&quot;,
   &quot;text&quot;: &quot;CEROUX Cabine&quot;
  },
  {
   &quot;value&quot;: &quot;7&quot;,
   &quot;text&quot;: &quot;CEROUX Chapelle aux Sabots&quot;
  },
  {
   &quot;value&quot;: &quot;8&quot;,
   &quot;text&quot;: &quot;CEROUX Place Communale&quot;
  },
  {
   &quot;value&quot;: &quot;9&quot;,
   &quot;text&quot;: &quot;CEROUX Quatre Bras&quot;
  },
  {
   &quot;value&quot;: &quot;10&quot;,
   &quot;text&quot;: &quot;Station Jambeaux&quot;
  },
  {
   &quot;value&quot;: &quot;11&quot;,
   &quot;text&quot;: &quot;Reseau Street&quot;
  },
  {
   &quot;value&quot;: &quot;12&quot;,
   &quot;text&quot;: &quot;beaux street&quot;
  }
  ]
  
  </code></pre>
  <p><strong>EDIT</strong></p>
  <p>This is fine, but in another case where I have this data transformed in something like this, this sort is not working</p>
  <pre><code>   {
        &quot;item&quot;:{
           &quot;value&quot;:&quot;1558&quot;,
           &quot;text&quot;:&quot;BEAUMONT Habitation 54&quot;
        },
        &quot;refIndex&quot;:0,
        &quot;matches&quot;:[
           {
              &quot;indices&quot;:[
                 [
                    0,
                    1
                 ]
              ],
              &quot;value&quot;:&quot;BEAUMONT Habitation 54&quot;,
              &quot;key&quot;:&quot;text&quot;
           }
        ],
        &quot;score&quot;:0.018533147937493524
     },
     {
        &quot;item&quot;:{
           &quot;value&quot;:&quot;1560&quot;,
           &quot;text&quot;:&quot;BEAUMONT Place de Renival&quot;
        },
        &quot;refIndex&quot;:3,
        &quot;matches&quot;:[
           {
              &quot;indices&quot;:[
                 [
                    0,
                    1
                 ]
              ],
              &quot;value&quot;:&quot;BEAUMONT Place de Renival&quot;,
              &quot;key&quot;:&quot;text&quot;
           }
        ],
        &quot;score&quot;:0.03162277660168379
     }
  ]
  </code></pre>
  <p>to make it work I made some changes to the code, but it doesn't seems to work.</p>
  <pre><code>function sortByInput(data, input = null) {
    if (!input) {
      return data.sort((a, b) =&gt; a.item.text.localeCompare(b.item.text));
    }
    
    return data.sort((a, b) =&gt; {
      const regex = new RegExp(`(^${input})`, &quot;i&quot;);
      const aMatch = regex.test(a.item.text);
      const bMatch = regex.test(b.item.text);
  
      if (aMatch || bMatch) return -aMatch + bMatch;
  
      return a.item.text.localeCompare(b.item.text);
    });
  }
  </code></pre>