简体   繁体   中英

Why google spreadsheets scripts can't sort?

function SUMMARIZE_TOGGL_ENTRIES(proj, desc, time, taskDates, uID) {
  if (!desc.map || !time.map || !taskDates.map || !proj.map) {
      return 'INPUT HAS TO BE MAP';
  }
  if (desc.length != time.length || time.length != taskDates.length || proj.length != taskDates.length) {
      return 'WRONG INPUT ARRAYS LEN';
  }
  var resObj = {'NO_DESCRIPTION': '00:00:00'};
  var keys = [];
  var result = [];
  for (var i = 1; i < desc.length; i++) {
      var tmpKey = createKey(proj[i], desc[i], uID[i]);
      console.log('tmpKey',tmpKey);
      if (resObj[tmpKey]) {
          resObj[tmpKey] = formatTime(timestrToSec(resObj[tmpKey]) + timestrToSec(time[i]));
      } else {
          resObj[tmpKey] = formatTime(timestrToSec(time[i]));
      }
  }
  keys = Object.keys(resObj);

  var b=0;
  for (b; b < keys.length; b++) {
      var tmp = [];
      var key = keys[b].split('__')[1];
      console.log('keys',keys[b].split('__'));
      tmp.push(keys[b].split('__')[0]);
      tmp.push(key);
      tmp.push(resObj[keys[b]]);
      tmp.push(taskLastDate(keys[b], desc, taskDates, proj, uID))
      tmp.push(keys[b].split('__')[2]);
    if(tmp[3] != '01-01-1991 01:01:01' && tmp[3] != ''){
      result.push(tmp);
    }

  }
  return result.sort(function (a,b){
    var ad = new Date(a[3].split(' ')[0].replace(/-/g,'.'));
    var bd = new Date(b[3].split(' ')[0].replace(/-/g,'.'));

    if (ad > bd) {
      return 1;
    }
    if (ad < bd) {
      return -1;
    }
    return 0;
  });
}

as you can see I'm trying to return a sorted array of arrays. But it don't returns the same array. If i lunch this func on my PC it works. so what's the problem? Does anyone have an idea?

result image

Seems like your sort function will always return 0. Change to

return result.sort(function (a,b){
var ad = new Date(a[3].split(' ')[0].replace(/-/g,'.'));
var bd = new Date(b[3].split(' ')[0].replace(/-/g,'.'));

if (ad > bd) {
  return 1;
} else if (ad < bd) {
  return -1;
} else {
  return 0;
}
});

and see if that works?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM