- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个需要排序的对象数组。如果对象具有类“search-filter-type”,则需要按特定顺序对其进行排序(参见:car_types_order)。对于数组中的所有其他对象,应按标签的字母顺序排序。我下面的内容有效,我想知道是否应该有两个单独的排序功能,或者是否有更简洁的方法来实现这一点?谢谢!
代码:
var car_types_order = ['Small Cars', 'Medium Cars', 'Large Cars', 'SUVs & Crossovers', 'Vans', 'Luxury',
'Convertibles', 'Sports', 'Wagons', 'Hybrids', 'Electric', 'Pickup Trucks', 'Off-Road Vehicles', 'RVs',
'Commercial', 'Specialty'];
filters.sort(function(a, b){
if (a.class == "search-filter-type" || b.class == "search-filter-type")
return car_types_order.indexOf(a.label) - car_types_order.indexOf(b.label);
else
{
if (a.label < b.label)
return -1;
if (a.label > b.label)
return 1;
}
return 0;
});
示例对象:
{title: "Rental Company", class: "search-filter-company", label: "Hertz", id: "filter-companies-HZ", value: "HZ"}
{title: "Rental Company", class: "search-filter-company", label: "Silvercar", id: "filter-companies-SC", value: "SC"}
{title: "Rental Company", class: "search-filter-company", label: "NextCar", id: "filter-companies-NC", value: "NC"}
{title: "Rental Company", class: "search-filter-company", label: "National", id: "filter-companies-NA", value: "NA"}
{title: "Rental Company", class: "search-filter-company", label: "Payless", id: "filter-companies-ZA", value: "ZA"}
{title: "Rental Company", class: "search-filter-company", label: "Sixt", id: "filter-companies-SX", value: "SX"}
{title: "Rental Company", class: "search-filter-company", label: "Thrifty", id: "filter-companies-ZT", value: "ZT"}
{title: "Car Type", class: "search-filter-type", label: "Commercial", id: "filter-types-commercial", value: "SKAR"}
{title: "Rental Company", class: "search-filter-company", label: "Routes", id: "filter-companies-RO", value: "RO"}
{title: "Car Type", class: "search-filter-type", label: "Electric", id: "filter-types-electric", value: "ICAE"}
{title: "Car Type", class: "search-filter-type", label: "Medium Cars", id: "filter-types-medium", value: "FCAR"}
{title: "Car Type", class: "search-filter-type", label: "Small Cars", id: "filter-types-small", value: "CCAR"}
最佳答案
您可以先检查是否给出了 'search-filter-type'
并将这些项目排序到 botton,如果都给出了,则按索引排序,如果相同,则按字母排序。
为了将 'search-filter-type'
排序到顶部,您可以在这一行中交换 a
和 b
:
(a.class === "search-filter-type") - (b.class === "search-filter-type")
var data = [{ title: "Rental Company", class: "search-filter-company", label: "Hertz", id: "filter-companies-HZ", value: "HZ" }, { title: "Rental Company", class: "search-filter-company", label: "National", id: "filter-companies-NA", value: "NA" }, { title: "Rental Company", class: "search-filter-company", label: "NextCar", id: "filter-companies-NC", value: "NC" }, { title: "Rental Company", class: "search-filter-company", label: "Payless", id: "filter-companies-ZA", value: "ZA" }, { title: "Rental Company", class: "search-filter-company", label: "Silvercar", id: "filter-companies-SC", value: "SC" }, { title: "Rental Company", class: "search-filter-company", label: "Sixt", id: "filter-companies-SX", value: "SX" }, { title: "Rental Company", class: "search-filter-company", label: "Thrifty", id: "filter-companies-ZT", value: "ZT" }, { title: "Car Type", class: "search-filter-type", label: "Commercial", id: "filter-types-commercial", value: "SKAR" }, { title: "Rental Company", class: "search-filter-company", label: "Routes", id: "filter-companies-RO", value: "RO" }, { title: "Car Type", class: "search-filter-type", label: "Electric", id: "filter-types-electric", value: "ICAE" }, { title: "Car Type", class: "search-filter-type", label: "Medium Cars", id: "filter-types-medium", value: "FCAR" }, { title: "Car Type", class: "search-filter-type", label: "Small Cars", id: "filter-types-small", value: "CCAR" }],
car_types_order = ['Small Cars', 'Medium Cars', 'Large Cars', 'SUVs & Crossovers', 'Vans', 'Luxury', 'Convertibles', 'Sports', 'Wagons', 'Hybrids', 'Electric', 'Pickup Trucks', 'Off-Road Vehicles', 'RVs', 'Commercial', 'Specialty'];
data.sort((a, b) =>
(a.class === "search-filter-type") - (b.class === "search-filter-type")
|| car_types_order.indexOf(a.label) - car_types_order.indexOf(b.label)
|| a.label.localeCompare(b.label)
);
console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }
一些更高级的技术:
使用一个对象来访问汽车类型的顺序。
为未找到的类型取默认值。这要求不要对已知类型使用虚假值,例如零。
var data = [{ title: "Rental Company", class: "search-filter-company", label: "Hertz", id: "filter-companies-HZ", value: "HZ" }, { title: "Rental Company", class: "search-filter-company", label: "National", id: "filter-companies-NA", value: "NA" }, { title: "Rental Company", class: "search-filter-company", label: "NextCar", id: "filter-companies-NC", value: "NC" }, { title: "Rental Company", class: "search-filter-company", label: "Payless", id: "filter-companies-ZA", value: "ZA" }, { title: "Rental Company", class: "search-filter-company", label: "Silvercar", id: "filter-companies-SC", value: "SC" }, { title: "Rental Company", class: "search-filter-company", label: "Sixt", id: "filter-companies-SX", value: "SX" }, { title: "Rental Company", class: "search-filter-company", label: "Thrifty", id: "filter-companies-ZT", value: "ZT" }, { title: "Car Type", class: "search-filter-type", label: "Commercial", id: "filter-types-commercial", value: "SKAR" }, { title: "Rental Company", class: "search-filter-company", label: "Routes", id: "filter-companies-RO", value: "RO" }, { title: "Car Type", class: "search-filter-type", label: "Electric", id: "filter-types-electric", value: "ICAE" }, { title: "Car Type", class: "search-filter-type", label: "Medium Cars", id: "filter-types-medium", value: "FCAR" }, { title: "Car Type", class: "search-filter-type", label: "Small Cars", id: "filter-types-small", value: "CCAR" }],
car_types_order = ['Small Cars', 'Medium Cars', 'Large Cars', 'SUVs & Crossovers', 'Vans', 'Luxury', 'Convertibles', 'Sports', 'Wagons', 'Hybrids', 'Electric', 'Pickup Trucks', 'Off-Road Vehicles', 'RVs', 'Commercial', 'Specialty'],
order = Object.assign({ default: 0 }, ...car_types_order.map((k, v) => ({ [k]: v + 1 })));
data.sort((a, b) =>
(a.class === "search-filter-type") - (b.class === "search-filter-type")
|| (order[a.label] || order.default) - (order[b.label] || order.default)
|| a.label.localeCompare(b.label)
);
console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }
关于javascript - 如何使用JS Sort对多个元素进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50121382/
我在玩一些代码挑战时发现自定义排序(排序接口(interface)的实现)比仅针对 slice 的原始结构要快得多。这是为什么?将 slice 转换为类型是否会产生一些魔力(例如转换为指向结构的指针
我正在使用 simple-import-sort eslint 插件进行 react 。我想我的 .eslintrc.js是对的,但我无法使这个特定的插件工作。我在文件的第一行收到以下错误: 未找到规
Closed. This question is not reproducible or was caused by typos。它当前不接受答案。 想改善这个问题吗?更新问题,以便将其作为on-to
好的,所以我是 Go 的新手,我正在努力让自己熟悉按函数排序。我可能误解了什么,所以如果我错了请纠正我。 我正在尝试创建一个包含字段 key 和 value 的 Nodes 数组。我想创建一个自定义排
我想从惰性列表中取出 n 个最大的元素。 我听说在 Data.List.sort 中实现的合并排序是惰性的,它不会产生不必要的元素。就比较而言,这可能是正确的,但在内存使用方面肯定不是这样。下面的程序
这个问题已经有答案了: Javascript sort function. Sort by First then by Second (10 个回答) 已关闭 3 年前。 我正在尝试返回已排序产品的列
我有一个 vector 对,如下所示。第一对值未排序,第二对值已排序(从零开始)。我可能想通过实现 std::vector 和 std::pair 来存储数据。当我有第一对值(未排序)时,找到相应的第
直到现在(Swift 2.2)我一直愉快地使用来自 this answer 的代码- 它迅速,优雅,它像梦一样工作。 extension MutableCollectionType where Ind
我在我的 Go 应用程序中实现排序界面时遇到问题。这是相关代码: type Group struct { Teams []*Team } type Team struct { Point
我很好奇 Lua 的默认算法是什么 table.sort使用,只是因为它比我遇到的其他一些排序算法慢。我也很好奇 Lua 的 table.sort是在引擎中用 C 编写的,或者如果它在 Lua 中的库
例如,插入排序被描述为部分排序数组的有效算法。但如何精确定义“部分排序”呢? 最佳答案 这是一个只有少数元素不合适的数组。如果没有指定百分比或其他阈值,则部分排序和未排序之间没有严格的区别。 正式定义
我是 GPU 编程的新手。最近,我正在尝试根据一个教程实现gpu bvh构建算法:http://devblogs.nvidia.com/parallelforall/thinking-parallel
有人可以指导我 Gnumeric 排序函数的详细说明(链接)吗? Gnumeric 手册很简短并且没有示例。我无法通过搜索引擎找到任何合适的信息,甚至 Stackoverflow 上也只有六个不合适的
在 Python 中使用什么精确规则来对列表进行排序,其中元素是列表?这可以表示为“key”或“cmp”吗功能?问题来自于有两件事考虑:长度和它们位置的值。 sorted([ [ 0, 1, 2
下面的代码应该创建一个整数数组 (a) 并对它进行排序,但是 sort.Sort 似乎没有修改变量。 package main import ( "fmt" "sort" ) type
我有一个应用于结构的自定义排序函数。完整代码是 here on play.golang.org . type Stmt struct { Name string After []st
python3 sorted取消了对cmp的支持。 python3 帮助文档: ?
以下是来自普林斯顿的 coursera 算法类(class)的练习。 如果一个数组既是 3 次排序又是 5 次排序,那么它是否也是 6 次、7 次、8 次、9 次和 10 次排序?我知道任何序列如果先
当我看到上面的语句时,我正在阅读 shell-sorting。这意味着什么?它对我看待 shell 排序的方式有何不同? PS:我不是在寻找声明的证据。 最佳答案 好吧,你可能暗示下一个排序阶段不会“
今天在检查mysql服务器的时候提示Sort aborted: Out of sort memory, consider increasing server sort buffer size,安装字
我是一名优秀的程序员,十分优秀!