Home General Never Sort Data Manually Again
General

Never Sort Data Manually Again

FlowSavers Team
~2 min
1-Click ready

1. Automatically sort your data by date and time...

Automatically sort your data by date and time

Text
  1. The Problem It happens to the best of us: you’ve managed to get your spreadsheet to sort itself by date, but now you need it to consider the time as well so things don’t become a visual mess. It’s frustrating to paste code snippets from tutorials without knowing which part to tweak when you want to add a second criterion and see nothing change in your spreadsheet.

  2. The Action Plan

  3. Hierarchy identification: Determine the primary column (date) and the secondary one (time) by assigning them an index number.

  4. Sorting command restructuring: Modify the Google Apps Script instruction so it stops reading a single criterion and starts processing a list of priorities.

  5. Range validation: Ensure the impact area (your table) remains fixed so the script doesn’t sort cells out of place.

  6. Detailed Implementation To solve this, you don’t need to learn programming; you just need to know “where to put the brackets.” The error in your current code is that you are giving it a simple instruction (a single column), whereas for multi-level sorting, Apps Script needs a list (an array).

Follow these technical steps:

  1. Open your Google Sheet and go to “Extensions” > “Apps Script.”
  2. Delete all the code currently in the editor.
  3. Copy and paste this new code, but pay close attention to the part I’ve marked below:
function onEdit(event){
  // 1. Define the worksheet
  var w = event.source.getSheetByName("Case Tracker");
  
  // 2. Identify the cell you just edited
  var c = event.range;
  
  // 3. Define which column triggers the sort (5 is your date)
  var srtby = 5;
  
  // 4. Define the total range we want to move (your table)
  var t = "A10:J115";

  // 5. Check if the edit was in the column of interest
  if(c.getColumn() == srtby){
    var range = w.getRange(t); // Note: We are using the range defined above
    
    // HERE IS THE TRICK:
    // Instead of using just {column: 5}, we use brackets [] to create a list of priorities.
    range.sort([
      { column: 5, ascending: true }, // First, sort by column 5 (Date)
      { column: 6, ascending: true }  // Second, sort by column 6 (Time)
    ]);
  }
}

Key points to ensure it works:

  • The “Time” column number: In the code, I set column: 6. If your time is in column F, it’s 6. If it’s in G, change it to 7. You just need to count from A (A=1, B=2, etc.).
  • The brackets [ ]: These are essential. Without them, the script only understands one instruction. With them, it understands a hierarchy of importance.
  • The range t = "A10:J115": If your table grows in the future, remember to extend this number so that data doesn’t get left out of the automatic sort.

Once saved, editing any cell in column 5 will group all identical dates together and, within each date, arrange the times chronologically. You’ve gained time and visual clarity without touching a single complex formula.

  1. Suggested Tool ChatGPT (Free or Plus version). It is the ideal tool for this case because it acts as a logic translator. If tomorrow you need the order to be descending or want to add a third column (for example, “Priority”), you don’t need to search for tutorials; just provide your current code and ask for the change. Its ability to understand JavaScript object structures (the language of Apps Script) is perfect for resolving syntax errors like this one.

  2. Prompt to copy

I have this Google Apps Script code that sorts a sheet named "Case Tracker" by column 5 when I edit that same column:

function onEdit(event){
var w = event.source.getSheetByName("Case Tracker");
var c = w.getActiveCell();
var srtby = 5;
var t = "A10:J115";
if(c.getColumn() == srtby){
var range = w.getRange(t);
range.sort( { column : srtby, ascending: true } );
}
}

I need to modify it so that when I edit column 5, the range "A10:J115" is sorted with two criteria: first by column 5 in ascending order and, as a second criterion, by column 6 in ascending order. Please provide the full code ready to copy and paste.
Share