Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
258 views
in Technique[技术] by (71.8m points)

javascript - sorting data that has blanks or gaps

I have data like this:

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

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;
});

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

 ] ;

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);
});
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Code:

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
});

Outputs:

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

Field 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); 
});

Dataset In:

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

Dataset Out:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...