我如何分类考虑大于和小于考虑?

时间:2017-09-01 01:10:33

标签: javascript sorting

我需要对两个字符串进行排序>和<符号考虑在内。因此,例如,排序可能看起来像

<20
<40
<100
0.1
10
1,000,000.75
>100
>1,000

所以基本上所有的字符串都用&lt;首先是正常排序,然后是所有带有&gt;的数字。符号。我也希望尊重所显示的确切顺序(例如,&gt; 100出现在&gt; 1,000之前从低到高排序)

这是我的代码,没有符号(排序表中的所有行):

if ($this.hasClass('sort-mixed')) {
  sort_func = sort_mixed;
}
$rows.sort(sort_func);

function sort_mixed(a, b) {

  var val_a = $(a).children().eq(column_index).text();
  var val_b = $(b).children().eq(column_index).text();

  val_a = Number(val_a.toString().replace(/,/g, ""));
  val_b = Number(val_b.toString().replace(/,/g, ""));  

  if(val_a > val_b) {
    return 1 * sort_direction;
  }
  if(val_a < val_b) {
    return -1 * sort_direction;
  }
  return 0;
}

3 个答案:

答案 0 :(得分:2)

这不是一个完整的解决方案,但足以让您入门。我们将数组分成多个部分,对每个部分进行排序,然后将数组重新组合在一起。

&#13;
&#13;
function toNumber(s) {
  return +s.replace(/[^0-9.]/g, '')
}
var arr = [
  '<20',
  '>1,000',
  '1,000,000.75',
  '<40',
  '0.1',
  '10',
  '<100',
  '>100'
];
var lt = arr
  .filter(s => s.startsWith('<'))
  .map(s => s.slice(1))
  .map(toNumber)
  .sort((a, b) => a - b)
  .map(n => '<' + n);
var eq = arr
  .filter(s => !s.startsWith('>') && !s.startsWith('<'))
  .map(toNumber)
  .sort((a, b) => a - b)
  .map(n => '' + n);
var gt = arr.filter(s => s.startsWith('>'))
  .map(s => s.slice(1))
  .map(toNumber)
  .sort((a, b) => a - b)
  .map(n => '>' + n);
console.log([].concat(lt, eq, gt));
&#13;
&#13;
&#13;

输出:

["<20", "<40", "<100", "0.1", "10", "1000000.75", ">100", ">1000"]

答案 1 :(得分:1)

使用单个比较器功能排序:

const order = { regular: 1, reverse: -1 };
let sortOrder = order.regular;
let strings = ['0.1', '1,000,000.75', '10', '<100', '<20', '<40', '>1,000', '>100'];

function compare(a, b) {
   function toNum(str) { return +str.replace(/[^0-9.-]/g, ''); }
   function range(str) { return { '<': -1, '>': 1 }[str[0]] || 0; }
   const rangeA = range(a);
   const rangeB = range(b);
   const score = rangeA === rangeB ? toNum(a) - toNum(b) : rangeA - rangeB;
   return score * sortOrder;
   }

strings.sort(compare);

函数range()检查字符串是以'<'还是'>'开头,如果字符串具有不同的范围,则设置用于排序的值。否则,字符串将转换为数字,并简单地按数字排序。

使用示例输入数据,生成的strings数组为:

["<20", "<40", "<100", "0.1", "10", "1,000,000.75", ">100", ">1,000"]

摆弄代码:
https://jsfiddle.net/fxgt4uzm

编辑:
添加了sortOrder

答案 2 :(得分:0)

组合方法

threads

Demo

对于你的情况..

// Sort function maker
// - create a sort fn based on two compare fns
//   {f}= primary
//   {g}= secondary
const sort = (f, g) => (a, b) => f(a,b) || g(a,b)

// Compare function maker
// - create a compare fn based on a weighting fn, {w}
const cmp_asc_of = (w) => (a, b) => w(a) - w(b)
const cmp_desc_of = (w) => (a, b) => w(b) - w(a)

// Weighting function
// - weight a given string, {string}, returns a number
const weight_op = (string) => ..
const weight_num = (string) => ..

// Then, create sort functions
const asc  = sort(cmp_asc_of(weight_op), cmp_asc_of(weight_num))
const desc = sort(cmp_asc_of(weight_op), cmp_desc_of(weight_num))

// Usage
array.sort(asc)
array.sort(desc)