title: Sort array with a search keyword - Javascript
body: <p>I need to sort a javascript array within the search result with the keyword which we searched. For example:</p>
  
  <pre><code>var profile = [
  {name: 'Arjun', age: 10},
  {name: 'Manu', age: 12},
  {name: 'Lipin', age: 15},
  {name: 'Anu', age: 11},
  {name: 'Anupama', age: 21},
  {name: 'Jijo', age: 34}
  ];
  </code></pre>
  
  <p>And If the keyword to be searched is "Anu".
  I want the result as follows.</p>
  
  <pre><code>[
  {name: 'Anu', age: 11},
  {name: 'Anupama', age: 21},
  {name: 'Manu', age: 12}
  ]
  </code></pre>
  
  <p>That is, I need to filter the array and sort the result with the keyword.</p>
  
  <p>Following is the code I have tried. The search is working fine. How can I filter with the search keyword?</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 profile = [
  {name: 'Arjun', age: 10},
  {name: 'Manu', age: 12},
  {name: 'Lipin', age: 15},
  {name: 'Anu', age: 11},
  {name: 'Anupama', age: 21},
  {name: 'Jijo', age: 34},
  {name: 'Abhiram', age: 22},
  {name: 'Ram', age: 20},
  {name: 'Ram Gopal', age: 21},
  {name: 'Ramachandran', age: 20},
  {name: 'Sreeram', age: 19},
  ];
  
  beneficiaryname = "Anu";
  result = profile.map(function(item, index){
   var existingname = 1;
   var row_ben_name = item.name;
   existingname = row_ben_name.toLowerCase().search(beneficiaryname.toLowerCase()) !== -1;
   if (existingname){
    return item;
   }
  });
  result = result.filter(function( element ) {
     return element !== undefined;
  });
  console.log(result);</code></pre>
  </div>
  </div>
  </p>
  
  <p>NOTE: I want to sort the array with the search keyword. Not as default A-Z sorting.</p>
  
  <blockquote>
    <p>UPDATE:</p>
  </blockquote>
  
  <p>For More Clarity, I have updated my question with more data and expected results.</p>
  
  <p>If I search with another keyword "Ram". I need the following result.</p>
  
  <pre><code>[
  {name: 'Ram', age: 20},
  {name: "Ram Gopal", age: 21},
  {name: "Ramachandran", age: 20},
  {name: "Abhiram", age: 22},
  {name: "Sreeram", age: 19}
  ]
  </code></pre>
  
  <p>See the above-expected result is sorted with the keyword searched.
  That is, In the sorted result, First we need to sort the exact search result, then keyword as first in string, then anywhere inside, then at the end of the string. I hope everyone got the problem.</p>