如何比较字符串和数字值(尊重负值,null总是在最后)? [英] How can one compare string and numeric values (respecting negative values, with null always last)?
问题描述
我正在尝试对值的数组进行排序,这些值可以是数字或字符串值的混合物(例如[10,"20",null,"1","bar","-2",-3,null,5,"foo"]
).我该如何对数组进行排序
I'm trying to sort an array of values that can be a mixture of numeric or string values (e.g. [10,"20",null,"1","bar","-2",-3,null,5,"foo"]
). How can I sort this array such that
-
null
值始终放在最后(无论排序顺序如何,请参阅jsFiddle) - 负数已正确排序(即,它们小于正数并在它们之间正确排序)
null
values are always placed last (regardless of sorting order, see jsFiddle)- negative numbers are sorted correctly (i.e. they are less than positive numbers and sort correctly amongst themselves)
?我制作了 jsFiddle ,其中包含详细的数字和字符串示例(使用localeCompare
和numeric
选项),但是会粘贴下面是我的排序算法的数字版本.
? I made a jsFiddle with detailed numeric and string examples (using localeCompare
and the numeric
option), but will paste the numeric version of my sorting algorithm below as a starting point.
// Sorting order
var order = "asc"; // Try switching between "asc" and "dsc"
// Dummy arrays
var numericArr = [10,20,null,1,-2,-3,null,5];
// Sort arrays
$(".output1").append(numericArr.toString());
numericArr.sort(sortByDataNumeric);
$(".output2").append(numericArr.toString());
// Numeric sorting function
function sortByDataNumeric(a, b, _order) {
// Replace internal parameters if not used
if (_order == null) _order = order;
// If values are null, place them at the end
var dflt = (_order == "asc" ? Number.MAX_VALUE : -Number.MAX_VALUE);
// Numeric values
var aVal = (a == null ? dflt : a);
var bVal = (b == null ? dflt : b);
return _order == "asc" ? (aVal - bVal) : (bVal - aVal);
}
我的字符串排序算法的问题(请参阅 jsFiddle )是我找不到始终放置null
值的最后一个和负值在其内部未正确排序(例如-3应该小于-2)
The problem with my string sorting algorithm (see jsFiddle) is that I can't find a way to always place null
values last and negative values aren't correctly sorted within themselves (e.g. -3 should be less than -2)
要回答评论,我希望[10,"20",null,"1","bar","-2",-3,null,5,"foo"]
可以排序为[-3,"-2","1",5,10,"20","bar","foo",null,null]
To answer the comments, I expect [10,"20",null,"1","bar","-2",-3,null,5,"foo"]
to sort to [-3,"-2","1",5,10,"20","bar","foo",null,null]
推荐答案
您应该首先检查两个值是否为null
,然后返回相反的值.
You should first check to see if either value is null
and return the opposite value.
旁注:
对于默认的_order
值,应检查参数是否为undefined
,而不是将其值与null
进行比较.如果您尝试直接比较未定义的内容,则会出现参考错误:
For your default _order
value, you should check if the parameter is undefined
instead of comparing its value to null
. If you try to compare something that is undefined directly you will get a reference error:
(undefinedVar == null) // ReferenceError: undefinedVar is not defined
相反,您应该检查变量是否未定义:
Instead, you should check if the variable is undefined:
(typeof undefinedVar == "undefined") // true
此外,将比较函数包装在闭包中而不是依赖于全局顺序变量可能是一个更好的主意.
Also, it's probably a better idea to wrap your compare function in a closure instead of relying on a global order variable.
有时像:
[].sort(function(a, b){ return sort(a, b, order)})
通过这种方式,您可以按实例进行排序.
This way you can sort at a per-instance level.
function sort(a, b, asc) {
var result;
/* Default ascending order */
if (typeof asc == "undefined") asc = true;
if (a === null) return 1;
if (b === null) return -1;
if (a === null && b === null) return 0;
result = a - b;
if (isNaN(result)) {
return (asc) ? a.toString().localeCompare(b) : b.toString().localeCompare(a);
}
else {
return (asc) ? result : -result;
}
}
这篇关于如何比较字符串和数字值(尊重负值,null总是在最后)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!