我有一个包含值的数组。我用一个条件对它进行排序,以保持某些项目位于顶部。到目前为止这是有效的。现在我想运行两个条件,例如我有两个前缀与数组中的每个项目匹配: tableprefix 和 secondaryprefix 。我已经实现的是将 tableprefix 保留在顶部。其余项目必须按字母顺序排序。
我想要实现的目标:
1:最顶层匹配tableprefix的数组项 //已经实现
2:匹配secondaryprefix的数组项目来自tableprefix //无法弄清楚
3:按字母顺序对其余项目进行排序 //已实现
阵列:
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";
排序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
}
}
});
排序结果:
[
"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"
]
现在我想要的是将所有与 tablePrefix "es_wl"
匹配的项目放在最顶层。但同时添加另一个前缀 secondaryPrefix "es_officer"
,以便所有匹配的项目都会在" es_wl"之后出现。项目,然后是通常的字母排序。
我已经查看了几个项目并应用了它,但项目甚至没有从他们的位置移动。我想我的逻辑完全错误,因为它集成了第二个前缀并保留了第一个前缀并在之后进行了排序。
答案 0 :(得分:2)
这是在这里完成的,运行代码片段。
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)

注意:您的排序代码存在问题 - 请查看es_officer
的排序,它不是按字母顺序排列的,我觉得应该是必需的。这一切都是在上面的片段中完成的。
更新:
要将id
列放在最上面,
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)

答案 1 :(得分:1)
你可以用regular expression来做。将数组转换为字符串,搜索带有前缀的单词:
var str = columns.toString(); // Array to string
var prefix = "es_wl";
var regex = new RegExp(prefix + "[\\w]+,", "g"); // Word with the prefix
var res = str.match(regex).sort(); // The array with the words that starts with the desired prefix
使用多个前缀,您可以将它们放在一个数组中。循环遍历数组,将多个正则表达式和concat()排序的数组放在一起。之后,删除带有replace()所需前缀的字符串中的单词:
str = str.replace(regex, "");
总计:
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 prefix1 = "es_wl";
var prefix2 = "es_officer";
var prefix3 = "es_department";
var prefixes1 = [prefix1, prefix2];
var prefixes2 = [prefix2, prefix1, prefix3];
function makeCustomOrder(prefixes, arr) {
var str = columns.toString();
var res = [];
prefixes.forEach((pre, i) => {
var regex = new RegExp(pre + "[\\w]+,", "g");
res = res.concat(str.match(regex).sort());
str = str.replace(regex, ""); // Delete the words with the desired prefixes
});
res = res.concat(str.split(",").sort()); // Put the sorted rest to the result
return res;
}
var res = makeCustomOrder(prefixes1, columns);
var res2 = makeCustomOrder(prefixes2, columns);
console.log(res); // Example 1
console.log(res2); // Example 2

契约函数makeCustomOrder()
的好处是您可以使用数组 前缀 尽可能多!