Sort By Custom Order

sortByCustomOrder(rows, colIndex, order) sorts a 2D array by a column's position in a custom list — for example Urgent, High, Medium, Low — instead of alphabetically. Values that are not in the list stay at the bottom, in their original order. The sort is stable, and the input array's order is left alone.

When to use

  • A priority, status, or stage column that should not sort A–Z
  • A menu button that re-sorts a task sheet the same way every time
  • Any list whose "right" order is one you define, not the alphabet

How to use this snippet

colIndex is 0-based (column A is 0). Comparison is trimmed and case-insensitive, so " high " matches "High".

/** * Sort rows by a column's position in a custom list. Values not in the list * are placed last, in their original order. The sort is stable. colIndex is * 0-based. Matching is trimmed and case-insensitive. Returns a new array; * the input array's order is unchanged. * * @param {Array<Array>} rows * @param {number} colIndex * @param {Array} order * @return {Array<Array>} */ function sortByCustomOrder(rows, colIndex, order) { if (!Array.isArray(rows)) { throw new Error('sortByCustomOrder: rows must be an array'); } if (typeof colIndex !== 'number' || colIndex < 0 || Math.floor(colIndex) !== colIndex) { throw new Error('sortByCustomOrder: colIndex must be an integer >= 0'); } var list = order || []; if (!Array.isArray(list)) { throw new Error('sortByCustomOrder: order must be an array'); } var rank = {}; for (var i = 0; i < list.length; i++) { var key = String(list[i] == null ? '' : list[i]).trim().toLowerCase(); if (!Object.prototype.hasOwnProperty.call(rank, key)) { rank[key] = i; } } var decorated = []; for (var r = 0; r < rows.length; r++) { var row = rows[r]; var raw = row && row[colIndex]; var lookup = String(raw == null ? '' : raw).trim().toLowerCase(); var pos = Object.prototype.hasOwnProperty.call(rank, lookup) ? rank[lookup] : list.length; decorated.push({ row: row, idx: r, pos: pos }); } decorated.sort(function (a, b) { if (a.pos !== b.pos) return a.pos - b.pos; return a.idx - b.idx; }); var out = []; for (var j = 0; j < decorated.length; j++) out.push(decorated[j].row); return out; }

Example

var PRIORITY = ['Urgent', 'High', 'Medium', 'Low']; function sortTasksByPriority() { var sheet = SpreadsheetApp.getActive().getSheetByName('Tasks'); var last = sheet.getLastRow(); var lastCol = sheet.getLastColumn(); if (last < 2) return; var header = sheet.getRange(1, 1, 1, lastCol).getValues()[0]; var col = header.indexOf('Priority'); if (col < 0) throw new Error('No Priority column'); var rows = sheet.getRange(2, 1, last - 1, lastCol).getValues(); var sorted = sortByCustomOrder(rows, col, PRIORITY); sheet.getRange(2, 1, sorted.length, lastCol).setValues(sorted); } function onOpen() { SpreadsheetApp.getUi() .createMenu('Tasks') .addItem('Sort by priority', 'sortTasksByPriority') .addToUi(); }

Notes

  • The helper sorts the array you give it. Leave the header row out, as the example does, or it will be sorted with the data.
  • If the same label appears twice in order, the first position wins.
  • setValues writes the reordered rows and leaves existing formatting in place.

Happy Coding!

NitroGAS Chrome Extension

Want this snippet handy inside the Apps Script editor? NitroGAS gives you free snippets — plus optional Co-Pilot when you want a boost (Day Pass — $3, or 1-week, 1-month, or yearly passes — no subscription). Happy Coding!

Get the Extension