Sorting array by multiple prefixes
<p>I have an array with values. I'm sorting it with a condition to keep certain items at the top. That is working so far. Now I want to run two conditions for example I've two prefixes to be matched with every item in the array: <em>tableprefix</em> and <em>secondaryprefix</em>. What I have already achieved is keeping <em>tableprefix</em> at the top. And the rest of the items must be sorted alphabetically.</p>

<p><strong>What I want to achieve:</strong></p>

<p><strong>1: Array items matching tableprefix at the very top</strong> // already achieved</p>

<p><strong>2: Array items matching secondaryprefix comes after tableprefix</strong> // can't figure out</p>

<p><strong>3: Sort rest of the items alphabetically</strong> // already achieved</p>

<p><strong>Array:</strong></p>

<pre><code>columns = [
   "es_officer_name",
   "es_officer_fname",
   "es_officer_apply_status",
   "es_officer_dob",
   "es_wl_1_11_test_id",
   "es_officer_id",
   "es_designation_id",
   "es_wl_1_11_test_edit_date",
   "es_designation_title",
   "es_employment_type_id",
   "es_employment_type_name",
   "es_service_type_id",
   "es_service_type_name",
   "es_wl_1_11_test_added_date",
   "es_bps_id",
   "es_bps_title",
   "es_department_id",
   "es_department_name"
];
</code></pre>

<p><strong>Prefix:</strong></p>

<pre><code>var tablePrefix = "es_wl";
</code></pre>

<p><strong>Sorting Algo:</strong></p>

<pre><code>columns.sort(function(a, b) 
{
    if (a.indexOf(tablePrefix))
    {
        if (b.indexOf(tablePrefix)) 
        {
            return a.localeCompare(b);
        } 
        else
        {
            return 1;
        }
    } 
    else
    {
        if (b.indexOf(tablePrefix)) 
        {
            return -1;
        }
        else 
        {
            return 0; // All prefixed are considered equal
        }
    }
});
</code></pre>

<p><strong>Sorted Result:</strong></p>

<pre><code>[
  "es_wl_1_11_test_id",
  "es_wl_1_11_test_edit_date",
  "es_wl_1_11_test_added_date",
  "es_bps_id",
  "es_bps_title",
  "es_department_id",
  "es_department_name",
  "es_designation_id",
  "es_designation_title",
  "es_employment_type_id",
  "es_employment_type_name",
  "es_officer_apply_status",
  "es_officer_dob",
  "es_officer_fname",
  "es_officer_id",
  "es_officer_name",
  "es_service_type_id",
  "es_service_type_name"
]
</code></pre>

<p><strong>Now what I want is to keep all the items matching the <em>tablePrefix</em> <code>"es_wl"</code> at the very top. But at the same time Add another prefix <em>secondaryPrefix</em> <code>"es_officer"</code> so that all matching items would come right after "es_wl" items and then the usual alphabetical sorting.</strong> </p>

<p>I have looked at several items and applied it but the items are not even moving from their place. I guess my logic is completely wrong for integrating second prefix and keeping the first one and sorting afterwards. </p>