排序有空白或空白的数据 [英] sorting data that has blanks or gaps
问题描述
我有这样的数据:
var dataset = [
{ "field1":"val1", "field2":"val2", "field3":"34.11" },
{ "field1":"val4", "field2":"val5", "field4":"2/3/2015" },
{ "field1":"val6", "field2":"val7", "field3": "26.37", "field4":"4/2/2015" }
] ;
如您所见,这些数据在第二行中丢失了"field3",而在第一行中丢失了"field4",那么我如何仅使用javascript来对这些数据进行排序?如果这行不通,是否有可能遍历数据集以在每一行中输入缺失的字段?因为我无法控制传入的数据.
As you see these data is missing "field3" in the second row and missing "field4" in the first row so how I can sort these data using javascript only ? if this wont work is it possible to loop through dataset to enter the missing fields in each row ? as I can not control the incoming data.
我能够对数据集进行字符串和数字排序,但前提是所有行都存在,例如,对于字符串列:
I was able to sort the dataset for strings and numbers but only if all rows are present for example this for string columns:
dataset.sort(function (a, b) {
var nameA = a.field1.toUpperCase(); // ignore upper and lowercase
var nameB = b.field1.toUpperCase(); // ignore upper and lowercase
if (nameA < nameB) {
return -1;
}
if (nameA > nameB) {
return 1;
}
return 0;
});
所需的输出是HTML表,但是我知道如何使用Javascript生成此表,因此我只希望能够根据列值类型对asc或desc的每个列分别进行排序.例如,对field3的升序排序应为
The desired output is HTML table but I know how to generate this table using Javascript so I just want to be able to sort each column separately asc or desc based on the column value type. for example sorting asc for field3 should be
var dataset = [
{ "field1":"val4", "field2":"val5", "field4":"2/3/2015" },
{ "field1":"val6", "field2":"val7", "field3": "26.37", "field4":"4/2/2015" },
{ "field1":"val1", "field2":"val2", "field3":"34.11" }
] ;
更新: 我的代码尝试根据field3进行排序,但排序不正确
Update: My code trying to sort based on field3 but I get incorrect sort
dataset.sort(function (a, b) {
var nameA = Number(a.field3);
var nameB = Number(b.field3);
return (nameB - nameA);
});
推荐答案
代码:
dataset.sort(function (a, b) {
var nameA = Number(a.field3)||0; //if value is NaN use 0
var nameB = Number(b.field3)||0; //if value is NaN use 0
return (nameA - nameB); //wrong order (nameB - nameA) returns greatest to least
});
输出:
[
{
"field1": "val4",
"field2": "val5",
"field4": "2/3/2015"
},
{
"field1": "val6",
"field2": "val7",
"field3": "26.37",
"field4": "4/2/2015"
},
{
"field1": "val1",
"field2": "val2",
"field3": "34.11"
}
]
字段4:
dataset.sort(function (a, b) {
var nameA = (new Date(a.field4));
if ( isNaN( nameA.getTime() ) ) { //checks if date is invalid
nameA = -10e15;
}
var nameB = (new Date(b.field4));
if ( isNaN( nameB.getTime() ) ) {
nameB = -10e15;
}
return (nameA - nameB);
});
数据集输入:
var dataset = [
{ "field1":"val4", "field2":"val5", "field4":"2/3/2015" },
{ "field1":"val6", "field2":"val7", "field3": "26.37", "field4":"4/2/2015" },
{ "field1":"val1", "field2":"val2", "field3":"34.11" },
{ "field1":"val1", "field2":"val2", "field3":"34.11", "field4":"4/1/2003" }
] ;
数据集输出:
[
{
"field1": "val1",
"field2": "val2",
"field3": "34.11"
},
{
"field1": "val1",
"field2": "val2",
"field3": "34.11",
"field4": "4/1/2003"
},
{
"field1": "val4",
"field2": "val5",
"field4": "2/3/2015"
},
{
"field1": "val6",
"field2": "val7",
"field3": "26.37",
"field4": "4/2/2015"
}
]
这篇关于排序有空白或空白的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!