创建自定义排序函数,作为脚本中的选项使用

Creating a custom sorting function for use as an option in script

本文关键字:选项 脚本 排序 自定义 函数 创建      更新时间:2023-09-26

我正在使用香草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),然后按这个顺序依次排序:冬季、秋季、春季/夏季。

是否有可能为这样的东西做一个自定义排序,如果是这样,如何用这个脚本实现?

如果ab之前,排序函数应该返回一个负数,如果不应该更改,则返回0,如果ba之前,则返回一个正数。下面是一个可能的实现,假设日期是一个格式为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;
}

您还应该首先验证日期字符串,并检查它是否为有效的季节。