[英]How do I sort taking greater than and less than into consideration?
I need to have a sort on two strings take the > and < symbols into consideration. 我需要对两个字符串进行排序,并考虑>和<符号。 So, for example, the sort might look like
因此,例如,排序可能看起来像
<20
<40
<100
0.1
10
1,000,000.75
>100
>1,000
So basically all the strings with < are first, followed by a normal sort, followed by all numbers with a > symbol. 所以基本上所有带<的字符串首先是正常排序,然后是带有>符号的所有数字。 I'd also like the sort to respect the exact order shown (eg >100 appears before >1,000 when sorted low to high)
我也想要尊重所显示的确切顺序(例如>从100到1000之前出现> 100时从高到高排序)
Here is my code that works without the symbols (sorting all rows in a table): 这是我的代码,没有符号(排序表中的所有行):
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;
}
Here is not a complete solution but enough to get you started. 这不是一个完整的解决方案,但足以让您入门。 We'll break the array into multiple parts, sort each part, then put the array back together.
我们将数组分成多个部分,对每个部分进行排序,然后将数组重新组合在一起。
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));
Outputs: 输出:
["<20", "<40", "<100", "0.1", "10", "1000000.75", ">100", ">1000"]
Sort with a single comparator function: 使用单个比较器功能排序:
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);
The function range()
checks if the string starts with a '<'
or '>'
and sets a value that is used for sorting if the strings have different ranges. 函数
range()
检查字符串是否以'<'
或'>'
开头,并设置一个值,如果字符串具有不同的范围,则用于排序。 Otherwise, the strings are converted to numbers and simply sorted as numbers. 否则,字符串将转换为数字,并简单地按数字排序。
With the example input data, the resulting strings
array is: 使用示例输入数据,生成的
strings
数组是:
["<20", "<40", "<100", "0.1", "10", "1,000,000.75", ">100", ">1,000"]
Fiddle with the code: 摆弄代码:
https://jsfiddle.net/fxgt4uzm https://jsfiddle.net/fxgt4uzm
Edited: 编辑:
Added sortOrder
. 添加了
sortOrder
。
Composition approach 组合方法
// 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)
For your case.. 对于你的情况..
..
function sort_mixed(a, b) {
var val_a = ..
var val_b = ..
return isHighToLow ? desc(val_a, val_b) : asc(val_a, val_b)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.