Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Apps script Sort

Using App Scripts I've pulled all the headers in an array, strip out the text part and built an array of the below format:
[1,10,23,45,7,12,76,88,9]

Sample code:

var shHeader = sh.getRange(1, 3, 1, 20);
var shH1 = shHeader.getValues();
shH1 = shH1[0].sort();

When I do the normal sort() on the array, the order I'm getting is like:
[1,10,12,23,45,7,76,88,9]

I want sort to give me an output:
[1,7,9,10,12,23,45,76,88]

Can anyone help with any function or some easy hack to get my desired results?

like image 700
Hakeem Avatar asked Jul 16 '26 23:07

Hakeem


1 Answers

You can pass a function into the sort method that takes two parameters.

shH1 = shH1[0].sort(function(a, b) {return a - b;});

Here is documentation to help you: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

like image 73
Jordan Rhea Avatar answered Jul 18 '26 18:07

Jordan Rhea



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!