Javascript 如何按剩余月数和天数对列进行排序
我有一个表格,在这个表格中我有一个列,显示了还有多少时间,还有几个月和几天。像这样:Javascript 如何按剩余月数和天数对列进行排序,javascript,sorting,date,validation,time,Javascript,Sorting,Date,Validation,Time,我有一个表格,在这个表格中我有一个列,显示了还有多少时间,还有几个月和几天。像这样: 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', '.s
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 + ')');
}
您可以生成一个单位计数递减的数组,并迭代这两个数组进行排序
函数getTime(s){
返回[+s.match(/\d+(?=\s*月\(s\)/)| | 0,+s.match(/\d+(?=\s*d\)/)| | 0];
}
变量数组=['6个月6天','11天','23个月25天','2天'];
array.sort((a,b)=>{
var aa=getTime(a),
bb=getTime(b),
三角洲;
aa.一些((v,i)=>delta=v-bb[i]);
返回三角洲;
});
console.log(数组)代码>共享您尝试过的代码???如果可能,我建议使用momentJS处理与时间、日期等相关的任何内容。您也可以从每个持续时间中生成一个数字,例如,months*31+days
,但这可以正常工作。;-)
Days left:
2 d.
6 month(s) 6 d.
11 d.
23 month(s) 25 d.