Hakeem
Hakeem

Reputation: 33

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?

Upvotes: 3

Views: 3313

Answers (1)

Jordan Rhea
Jordan Rhea

Reputation: 1206

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

Upvotes: 2

Related Questions