创建自定义排序函数,作为脚本中的选项使用
Creating a custom sorting function for use as an option in script
我正在使用香草JS脚本list.js,并试图实现自定义排序功能。
我一直在使用列表API中的sort
方法,没有正常排序的问题,但是需要为几个情况创建自定义排序,但是因为他从未提供过示例,我不确定如何做到这一点。
例如我有这样的代码:
var options = {
valueNames: [ 'date', 'amount', 'company', 'name' ]
};
// Instantiate our list
var myList = new List('mylist', options);
// Set our default sort
myList.sort('date', {order: "desc"});
// Control our own sorting via the links
$('a.sort').on('click', function(event) {
// Prevent default action
event.preventDefault();
var element = $(event.target);
// Get sorting method
var method = element.attr('data-sort');
// Work out what way we want to order the results
switch (method) {
case 'date':
case 'amount':
var order = 'desc';
break;
case 'company':
case 'name':
var order = 'asc';
break;
default:
var order = 'desc';
}
// Sort the data
myList.sort(method, {order: order, sortFunction: function(a, b, options){
}});
return false; // IE 8
});
他从未说明该函数的预期签名,但我发现它有3个参数填充数据,前两个似乎包含排序列表中的元素,第三个有某种选项。
我读过关于使用自定义排序函数的文章,但不确定如何在这里补充它们。
我们需要的一个排序示例是,我们有一个日期列表,如:- 2015年春夏
- 2014年春夏
- 2013年冬季
- 2005年秋季
应该先按最高的数字排序(在本例中为2015),然后按这个顺序依次排序:冬季、秋季、春季/夏季。
是否有可能为这样的东西做一个自定义排序,如果是这样,如何用这个脚本实现?
如果a
在b
之前,排序函数应该返回一个负数,如果不应该更改,则返回0,如果b
在a
之前,则返回一个正数。下面是一个可能的实现,假设日期是一个格式为YYYY {{season}}
的可预测字符串。
// Putting the season in the keys for ease of use and assigning them values so we know which one comes first.
var seasonMap = {
'Spring/Summer': 0,
Winter: 1,
Fall: 2
};
var order = 'asc'; // ascending or descending
function sortFunction(a, b) {
var coefficient = order === 'asc' ? 1 : -1; // If the order you pass it is 'asc', the a positive coefficient will retain this function's ascending sorting order; otherwise it will invert this function.
// Convert the dates into arrays by splitting them by a space ' '
var aDateComponents = a._values.date.split(' '),
bDateComponents = b._values.date.split(' ');
// The 0th value of the array is the year; additionally convert it into a Number instead of a String
var aYear = Number(aDateComponents[0]),
bYear = Number(bDateComponents[0]);
// The 1st value of the array is the season; look up the value from the map object we created in the beginning so we know which season comes after which
var aSeasonNumber = seasonMap[aDateComponents[1]],
bSeasonNumber = seasonMap[bDateComponents[1]];
if (aYear !== bYear) {
// return aYear - bYear. If a > b this will put a after b. The coefficient will invert it if we're sorting descending.
return coefficient * (aYear - bYear);
}
if (aSeasonNumber !== bSeasonNumber) {
// Same thing here. Since the season was mapped to a number, we know which season comes first and can return a positive or negative difference.
return coefficient * (aSeasonNumber - bSeasonNumber);
}
// Don't change the order otherwise.
return 0;
}
您还应该首先验证日期字符串,并检查它是否为有效的季节。
相关文章:
- 选项卡侦听器未被来自后台脚本的消息激活
- Dynamics 2016内部部署Crm客户端脚本.选项集控件类型缺少方法
- 使用java脚本更改onClick事件中菜单选项卡的颜色
- 在服务器端脚本执行后关闭选项卡
- 引导程序动画选项卡内容脚本
- 使用内容脚本中的“简单首选项”打开插件的选项页
- 使用Ajax或JavaScript选择下拉列表选项后,在同一页面上执行PHP脚本
- 编写脚本以关闭其他选项卡或浏览器
- ExternalInterface为我提供了在使用多个参数时停止脚本的选项.为什么?
- Javascript:更改<选择>java脚本中的选项
- 如何防止用户使用浏览器选项禁用脚本
- 设置间隔未在选项卡内容脚本中调用
- 如何创建一个“下一步”按钮,使用 Java 脚本和 Twitter Bootstrap 切换选项卡
- 我们是否有任何通用脚本/ jquery插件可以在通过ajax选项卡加载的内容上应用javascript
- 如何在同一选项卡中打开新脚本
- Chrome扩展-重定向当前选项卡后重新运行扩展脚本
- 从内容脚本打开chrome选项卡
- jQuery选项卡的自定义脚本
- 如何在chrome扩展中的新选项卡上加载url覆盖的脚本
- 如何在脚本中选择之前获取所有选项id值