gpt4 book ai didi

javascript - 如何按剩余的月数和天数对列进行排序

转载 作者:行者123 更新时间:2023-11-30 11:20:47 24 4
gpt4 key购买 nike

我有一个表格,在该表格中我有一列显示剩余的月份和天数。像这样:

Days left:
6 month(s) 6 d.
11 d.
23 month(s) 25d.
2 d.

我需要一个能够识别月份、日期并按升序或降序对它们进行排序的 JavaScript,如下所示:

Days left:
2 d.
11 d.
6 month(s) 6 d.
23 month(s) 25 d.

我使用这段代码:

/* Table Sort */

$('body').on('click', '.sort-table th', function() {
// sorts only if th has class 'sortable'
if ($(this).hasClass('sortable')) {
// toggle caret up/down
$(this).children('.fa-caret-down, .fa-caret-up').toggleClass('fa-caret-down').toggleClass('fa-caret-up');
var table = $(this).parents('table').eq(0);
// use dynamic method as sort comparison function
var compare = createDynamicMethod(this);
var rows = table.find('tr:gt(0)').toArray().sort(compare($(this).index()));
this.asc = !this.asc;
if (!this.asc) {
rows = rows.reverse()
}

for (var i = 0; i < rows.length; i++) {
table.append(rows[i])
}
}
});

// compares images,th should have class 'sort-img'
function imageCompare(index) {
return function(a, b) {
var valA = imageValue(a, index);
var valB = imageValue(b, index);

return valA.toString().localeCompare(valB);
}
}

// get image value for comparison (by attribute 'alt')
function imageValue(a, index) {
var valA = $(a).children('td').eq(index).children('img').attr('alt');

return valA ? valA : '';
}

// compares amounts (15 kg, 5 men., 41% etc.), th should have class 'sort-amount'
function amountCompare(index) {
return function(a, b) {
var valA = amountValue(a, index);
var valB = amountValue(b, index);

return valA - valB;
}
}

// get amount value (number without measurement units)
function amountValue(a, index) {
var td = $(a).children('td').eq(index);
// some values in template are surrounded by <b> tags, fix
var valA = $(td).children().length > 0 ? $(td).children().first().html() : $(td).html();

return /([0-9]+\.[0-9]+)|([0-9]+)/.exec(valA)[0];
}

// compares simple numbers or text, for th with only 'sortable' class
function defaultCompare(index) {
return function(a, b) {
var valA = defaultValue(a, index);
var valB = defaultValue(b, index);

return $.isNumeric(valA) && $.isNumeric(valB) ? valA - valB : valA.toString().localeCompare(valB);
}
}

// get simple number or text value
function defaultValue(a, index) {
var td = $(a).children('td').eq(index);
// if td has children (for example <a>), return first child value
var valA = $(td).children().length > 0 ? $(td).children().first().html() : $(td).html();

valA = valA.replace(/[^\-a-zA-Z0-9]/g,'');

return valA == '' ? 0 : valA;
}

// compares progress bar with amount invested values, th should have class 'sort-bar'
function barCompare(index) {
return function(a, b) {
var valA = barValue(a, index);
var valB = barValue(b, index);

return valA.localeCompare(valB);
}
}

// get value in progress bar ('xx€ / xx€')
function barValue(a, index) {
return $(a).children('td').eq(index).find('span').html();
}

// compares amounts (15 kg, 5 men., 41% etc.), th should have class 'sort-amount'
function complexCompare(index) {
return function(a, b) {
var valA = complexValue(a, index);
var valB = complexValue(b, index);

return valA - valB;
}
}

// get complex value
function iconValue(a, index) {
var td = $(a).children('td').eq(index);
// some values in template are surrounded by <b> tags, fix
var valA = $(td).html();

valA = valA.replace(/[^\-a-zA-Z0-9]/g,'');

return valA != null && valA != '' ? 1 : 0;
}

// compares amounts (15 kg, 5 men., 41% etc.), th should have class 'sort-amount'
function iconCompare(index) {
return function(a, b) {
var valA = iconValue(a, index);
var valB = iconValue(b, index);

return valA - valB;
}
}

// get complex value
function complexValue(a, index) {
var td = $(a).children('td').eq(index);
// some values in template are surrounded by <b> tags, fix
var valA = $(td).children().length > 0 ? $(td).children().first().html() : $(td).html();
valA = valA.replace(/[^\-a-zA-Z0-9]/g,'');
var valR = /([0-9]+\.[0-9]+)|([0-9]+)/.exec(valA);

return valR != null ? valR[0] : 0;
}

/*
* creates dynamic method that is used for row sorting
* accepts th element and looks for class 'sort-<smth>'
* then creates method <smth>Compare
*/
function createDynamicMethod(elem) {
var classnames = /sort-[a-z]+/.exec($(elem).attr('class'));
var method = (classnames !== null) ? classnames[0] : 'sort-default';
method = method.substring(5) + 'Compare';

return eval('(' + method + ')');
}

但它不识别月份和日期,而是按第一个数字对它们进行排序:

Days left:
2 d.
6 month(s) 6 d.
11 d.
23 month(s) 25 d.

最佳答案

您可以生成一个单元数递减的数组,然后迭代两个数组进行排序。

function getTime(s) {
return [+s.match(/\d+(?=\s*month\(s\))/) || 0, +s.match(/\d+(?=\s*d\.)/) || 0];
}

var array = ['6 month(s) 6 d.', '11 d.', '23 month(s) 25d.', '2 d.'];

array.sort((a, b) => {
var aa = getTime(a),
bb = getTime(b),
delta;

aa.some((v, i) => delta = v - bb[i]);
return delta;
});

console.log(array);

关于javascript - 如何按剩余的月数和天数对列进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49919436/

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