按数字顺序使用逗号分隔的价格字符串对对象数组进行排序
Sorting array of objects with comma-delimited price strings in numeric order
我正在使用 this solution 对对象数组进行排序。这是函数:
function sortJsonArrayByProperty(objArray, prop, direction){
if (arguments.length<2) throw new Error("sortJsonArrayByProp requires 2 arguments");
var direct = arguments.length>2 ? arguments[2] : 1; //Default to ascending
if (objArray && objArray.constructor===Array){
var propPath = (prop.constructor===Array) ? prop : prop.split(".");
objArray.sort(function(a,b){
for (var p in propPath){
if (a[propPath[p]] && b[propPath[p]]){
a = a[propPath[p]];
b = b[propPath[p]];
}
}
// convert numeric strings to integers
a = a.match(/^\d+$/) ? +a : a;
b = b.match(/^\d+$/) ? +b : b;
return ( (a < b) ? -1*direct : ((a > b) ? 1*direct : 0) );
});
}
}
这是一个很好的解决方案。
但我对以这种格式存储 价格 的列有疑问:
950,75
1234,99
500,00
所以,我的值用 逗号 分隔小数点。
然后,而不是这个序列:
222,55
550,00
2000,99
3000,00
我得到:
2000,99
222,55
3000,00
550,00
我正在尝试在这部分做一些修改:
a = a.match(/^\d+$/) ? +a : a;
b = b.match(/^\d+$/) ? +b : b;
但这不起作用。怎么了?
JavaScript 无法将逗号识别为有效垃圾数字的一部分,因此您不能使用一元 + 或任何其他内置方法转换为数字。您需要用句号替换逗号,然后强制转换为数字。
a = a.match(/^(\d+),(\d+)$/) ? +(a[1]+'.'+a[2]) : a;
在 JavaScript 中,小数点分隔符始终是 .
,而不是某些语言环境中的 ,
。因此,要将使用 ,
作为小数的数字字符串转换为 JavaScript 数字,您可以这样做:
theNumber = +theString.replace(/\./g, '').replace(/,/g, '.');
或
theNumber = parseFloat(theString.replace(/\./g, '').replace(/,/g, '.'));
...取决于您是否要忽略尾随的无效字符(+
不会,parseFloat
会)。
因此建议:
aVal = +a.replace(/\./g, '').replace(/,/g, '.');
bVal = +b.replace(/\./g, '').replace(/,/g, '.');
if (!isNaN(aVal) && !isNaN(bVal)) {
a = aVal;
b = bVal;
}
我正在使用 this solution 对对象数组进行排序。这是函数:
function sortJsonArrayByProperty(objArray, prop, direction){
if (arguments.length<2) throw new Error("sortJsonArrayByProp requires 2 arguments");
var direct = arguments.length>2 ? arguments[2] : 1; //Default to ascending
if (objArray && objArray.constructor===Array){
var propPath = (prop.constructor===Array) ? prop : prop.split(".");
objArray.sort(function(a,b){
for (var p in propPath){
if (a[propPath[p]] && b[propPath[p]]){
a = a[propPath[p]];
b = b[propPath[p]];
}
}
// convert numeric strings to integers
a = a.match(/^\d+$/) ? +a : a;
b = b.match(/^\d+$/) ? +b : b;
return ( (a < b) ? -1*direct : ((a > b) ? 1*direct : 0) );
});
}
}
这是一个很好的解决方案。
但我对以这种格式存储 价格 的列有疑问:
950,75 1234,99 500,00
所以,我的值用 逗号 分隔小数点。 然后,而不是这个序列:
222,55 550,00 2000,99 3000,00
我得到:
2000,99 222,55 3000,00 550,00
我正在尝试在这部分做一些修改:
a = a.match(/^\d+$/) ? +a : a;
b = b.match(/^\d+$/) ? +b : b;
但这不起作用。怎么了?
JavaScript 无法将逗号识别为有效垃圾数字的一部分,因此您不能使用一元 + 或任何其他内置方法转换为数字。您需要用句号替换逗号,然后强制转换为数字。
a = a.match(/^(\d+),(\d+)$/) ? +(a[1]+'.'+a[2]) : a;
在 JavaScript 中,小数点分隔符始终是 .
,而不是某些语言环境中的 ,
。因此,要将使用 ,
作为小数的数字字符串转换为 JavaScript 数字,您可以这样做:
theNumber = +theString.replace(/\./g, '').replace(/,/g, '.');
或
theNumber = parseFloat(theString.replace(/\./g, '').replace(/,/g, '.'));
...取决于您是否要忽略尾随的无效字符(+
不会,parseFloat
会)。
因此建议:
aVal = +a.replace(/\./g, '').replace(/,/g, '.');
bVal = +b.replace(/\./g, '').replace(/,/g, '.');
if (!isNaN(aVal) && !isNaN(bVal)) {
a = aVal;
b = bVal;
}