Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

161
Visualizações
How to sort in appscript?

im trying to sort column "a" then sort column "b".

I have a column called "Categories", im ordering it alphabetically. Then I want to order "Subcategories" alphabetically too, respecting the first column order.

function autoSort() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet=ss.getSheetByName("Table");
  const orderArray = sheet.getRange(2, 1, sheet.getLastRow()-1,sheet.getLastColumn()).getValues()
  orderArray.sort(sortFunction)
  sheet.getRange(2,1,sheet.getLastRow()-1,sheet.getLastColumn()).setValues(orderArray)
}

This is the function i use to sort the first column. How can I do to sort the second column?

var sortFunction= function(a,b){
  if (a[4]>b[4]){return 1}
  else if (b[4]>a[4]){return -1}
  return 0
}
about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

As an alternative you could always use the Range.sort() method.

function autoSort() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet=ss.getSheetByName("Table");
  const range = sheet.getRange(2, 1, sheet.getLastRow()-1,sheet.getLastColumn());
  range,sort([{column: 1, ascending: true}, {column: 2, ascending: true}])
}
about 4 years ago · Santiago Trujillo Relatório

0

The sort can be written to sort the second column(say Col6) instead of returning 0:

var sortFunction= function(a,b){
  if (a[4]>b[4]){return 1}
  else if (b[4]>a[4]){return -1}
  // they're equal:
  if (a[5]>b[5]){return 1}
  else if (b[5]>a[5]){return -1}
  return 0
}
about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda