gpt4 book ai didi

javascript - 按多个前缀对数组进行排序

转载 作者:行者123 更新时间:2023-11-29 19:09:34 25 4
gpt4 key购买 nike

我有一个包含值的数组。我正在对它进行排序,以将某些项目保留在顶部。到目前为止,这是有效的。现在我想运行两个条件,例如我有两个前缀要与数组中的每个项目匹配:tableprefixsecondaryprefix。我已经实现的是将 tableprefix 保持在顶部。其余项目必须按字母顺序排序。

我想要实现的目标:

1: Array items matching tableprefix at the very top//已经实现

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

排序算法:

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”项之后,然后按通常的字母顺序排序。

我查看了几个项目并应用了它,但这些项目甚至没有从它们的位置移动。我想我的逻辑对于集成第二个前缀并保留第一个并在之后进行排序是完全错误的。

最佳答案

到这里就完成了,运行代码段。

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)

关于javascript - 按多个前缀对数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40197499/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com