如何按列剩余的几个月和几天对列进行排序

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何按列剩余的几个月和几天对列进行排序相关的知识,希望对你有一定的参考价值。

我有一张桌子,在那张桌子上我有一个栏目,显示剩下几个月和几天的时间。像这样:

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);

以上是关于如何按列剩余的几个月和几天对列进行排序的主要内容,如果未能解决你的问题,请参考以下文章

如何在java中获得前一个月和几年?

如何获得MONTHS,DAYS和周(android datepicker)

一个时间插件是如何做到前几个月和后个几月的设置

对矩阵按行和按列进行排序

结构的选择排序

是否可以让日期数据类型接受 13 作为一个月和 30 作为所有月份?