Wcan Wcan - 9 months ago 39
Javascript Question

Javascript triple sorting by specific keys

I have an array of item which I'm sorting 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 array, tableprefix and secondaryprefix, what i have already achieved is keeping tableprefix at the top but also sort rest of the items alphabetically.

What I want to achieve:

1: Array items matching tableprefix at the very top // already achieved

2: Array items matching secondaryprefix comes after tableprefix // can't figure out

3: Sort rest of the items alphabetically // already achieved

Array:

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"
];


Prefix:

var tablePrefix = "es_wl";


Sorting Algo:

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
}
}
});


Sorted Result:

[
"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"
]


Now what I want is to Keep all the items matching the tablePrefix "es_wl" at the very top. but at the same time Add another prefix secondaryPrefix "es_officer" so that all matching items would come right after "es_wl" items and then the usual alphabetical sorting.

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.

A.J A.J
Answer Source

It is done here, run the snippet.

var 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"
];
var tablePrefix = "es_wl";
var secondTablePrefix = "es_officer"
columns.sort((function(firstPattern, SecondPattern) {
  this.sorting = function(a, b, tablePrefix1, primarySort) {
    if (a.indexOf(tablePrefix1) != -1) {
      if (b.indexOf(tablePrefix1) != -1) {
        return a.localeCompare(b);
      } else {
        return -1;
      }
    } else if (b.indexOf(tablePrefix1) != -1) {
      return 1;
    } else {
      return primarySort ? 0 : a.localeCompare(b); // All prefixed are considered equal
    }
  }
  return function(a, b) {
    var result = sorting(a, b, firstPattern, 1);
    if (result == 0) {
      result = sorting(a, b, SecondPattern, 0);
    }
    return result;
  }
})(tablePrefix, secondTablePrefix));

console.log(columns)

Note: There is an issue in your sorting code - look at the sorting of es_officer it is not alphabetical, I feel even that should be required. It is all done in snippet above.

UPDATE :

To get the id column in top,

var columns = [
  "es_officer_name",
  "es_officer_fname",
  "es_officer_apply_status",
  "es_officer_dob",
  "es_officer_id",
  "es_designation_id",
  "es_wl_1_11_test_edit_date",
  "es_designation_title",
  "es_employment_type_id",
  "es_wl_1_11_test_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"
];
var tablePrefix = "es_wl";
var secondTablePrefix = "es_officer"
columns.sort((function(firstPattern, SecondPattern) {
  this.sorting = function(a, b, tablePrefix1, primarySort) {
    if (a.indexOf(tablePrefix1) != -1) {
      if (b.indexOf(tablePrefix1) != -1) {
        return a.indexOf("_id") != -1 ? -1 : b.indexOf("id") != -1 ? 1 : a.localeCompare(b);
      } else {
        return -1;
      }
    } else if (b.indexOf(tablePrefix1) != -1) {
      return 1;
    } else {
      return primarySort ? 0 : a.localeCompare(b); // All prefixed are considered equal
    }
  }
  return function(a, b) {
    var result = sorting(a, b, firstPattern, 1);
    if (result == 0) {
      result = sorting(a, b, SecondPattern, 0);
    }
    return result == "retainIt" ? 0 : result;
  }
})(tablePrefix, secondTablePrefix));

console.log(columns)