对具有空白或间隙的数据进行排序

时间:2016-12-24 00:21:49

标签: javascript sorting

我有这样的数据:

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对这些数据进行排序?如果这不起作用,是否可以循环数据集以输入每行中缺少的字段?因为我无法控制传入的数据。

我能够为字符串和数字排序数据集,但仅当所有行都存在时才会对字符串列进行排序:

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排序asc应为

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进行排序,但是我的排序不正确

dataset.sort(function (a, b) {

  var nameA = Number(a.field3); 
  var nameB = Number(b.field3); 

  return (nameB - nameA);
});

2 个答案:

答案 0 :(得分:1)

代码:

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"
  }
]

答案 1 :(得分:0)

这是一般的排序功能。

如果您缺少字段,则此字段首先显示。否则 - 订单将基于相同字段值的localeCompare

&#13;
&#13;
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" }
 ] ;

dataset.sort(function (a, b) {
  aFields = Object.keys(a).sort()
  bFields = Object.keys(b).sort()
  i = 0;
  if (aFields[i] == bFields[i]) {
    if (a[aFields[i]].localeCompare(b[bFields[i]]) == 0) {
      i++;
    } else {
      return a[aFields[i]].localeCompare(b[bFields[i]]);
    }
  } else {
    return bFields[i].localeCompare(aFields[i]);
  }
});

console.log(dataset)
&#13;
&#13;
&#13;