1

I'm trying to get three columns sorted in an array in Google Apps Script. I realize there are lots of explanations, but I'm not getting it. I was hoping someone could troubleshoot the code snipper below.

Desired functionality: sort an array using 'columns' 8,9,10 (text and nulls), in order.

Issue: nulls don't seem to get sorted properly.

Code snippet:

shdv.sort(function(a,b){
  var so = -1
a[10]>b[10] ? so=1 : null; // section
a[10] == b[10] && (a[9]>b[9] || a[9] == '') ? so=1 : null;
a[10] == b[10] && a[9]==b[9] && a[8]>a[9] ? so=1 : null;
null;
})

Example result

[Not Tracking, Site, 2.85208117E8]
[Not Tracking, , 2.83812926E8]
[Not Tracking, , 2.83991529E8]
[Not Tracking, Site, 2.83812602E8]

Desired result

[Not Tracking, Site, 2.85208117E8]
[Not Tracking, Site, 2.83812602E8]
[Not Tracking, , 2.83812926E8]
[Not Tracking, , 2.83991529E8]
4
  • 1
    Provide minimal reproducible example. Your sort function has typos and doesn't return anything. Also provide sample values for shdv Commented Jan 4, 2021 at 18:44
  • Can't you use range.sort([{column:8,ascending:true},{column:9,ascending:true},{column:10,ascending:true}]) Example here Commented Jan 4, 2021 at 18:58
  • typo in code fixed... Commented Jan 4, 2021 at 19:01
  • @Cooper - thanks but this is for array only / no spreadsheet calls to fix this. Commented Jan 4, 2021 at 19:03

2 Answers 2

2

Explanation:

  • You are trying to sort based on undefined values in the array.

  • You can directly compare the elements like that: a[1]==undefined, b[1]==undefined;

Code snippet:

  const shdv = [
    ["Not Tracking", "Site", 2.85208117E8],
    ["Not Tracking", , 2.83812926E8],
    ["Not Tracking", , 2.83991529E8],
    ["Not Tracking", "Site", 2.83812602E8]
    ];
  
  shdv.sort( (a,b) => 
            // if same sort equally
            a[1]==b[1]?0:
            //if nulls put them in the bottom
            a[1]==undefined?1: 
            b[1]==undefined?-1:
            //sort based on 1st and 3rd element
            a[0].charCodeAt(0)-b[0].charCodeAt(0) || a[2]<b[2]  );
  console.log(shdv);

The console.log(shdv) in GAS returns the expected result:

enter image description here

References:

Array.prototype.sort()

Sign up to request clarification or add additional context in comments.

2 Comments

is there any potential issue with just doing a[1]==undefined?1: instead of ?1:null; the full argument and line close seems necessary. does documentation say all that is optional?
Not sure what you mean, but you can try it out if it works for you that way. However, I do prefer structured (complete) code so it is easy to be explained, maintained, understood. If less code works for you, then feel free to do it. But I don't think you will get more performance doing so in this scenario. @JasonTorpy Feel free to accept the answer if it solved your issue.
0

fix is to add at the end

return so;

so the sort actually sees the result. But I'm interested in any suggestions about how to do this better (array only / no spreadsheet calls)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.