Help request: sort by dueDate

I modified the alphabetical sort script to sort by dueDate. It works, except that it puts tasks without a due date at the top. I would like them to show up after the ones that do have a due date.

I’d love to get some help! Here is the script as it stands now. Thanks in advance.

– Henri

	"type": "action",
	"targets": ["omnifocus"],
	"author": "Picciotto Automator",
	"identifier": "",
	"version": "1.0",
	"description": "This action will sort the tasks of the selected non-sequential project in due date order.",
	"label": "Date-Sort Project Tasks",
	"shortLabel": "Date-Sort"
(() => {
	var action = new PlugIn.Action(function(selection, sender){
		// action code
		// selection options: tasks, projects, folders, tags
		var project = selection.projects[0]
		if (project.containsSingletonActions){
			var tasks = project.task.children
			if (tasks.length > 1){
				tasks.sort((a, b) => {
				  var x = a.dueDate;
				  var y = b.dueDate;
				  if (x < y) {return -1;}
				  if ((x > y) || (x = null)) {return 1;}
				  return 0;
				moveTasks(tasks, project)

	action.validate = function(selection, sender){
		// validation code
		// selection options: tasks, projects, folders, tags
		return (selection.projects.length === 1)
	return action;

Enclose your code between triple backticks, please.


// Code
// ...


That script would only sort top-level tasks. Are you aware of that ? Is it your goal ?

Within a project, I only have top-level tasks. When things are more complicated, I use OmniOutliner.
(And, by the way, if I ever have a dueDate sort that works, I hope I can use it in OmniOutliner, as its Sort command has the same problem when it comes to dates.)

Per your request, I’ve added to my repository the file Reorder (top-level) tasks of first selected project.omnifocusjs (inside OmniFocus 3 & 4 folder). Does it accomplish what you want ?

Yes, exactly. Thank you!

Is there a way to use some version of this in OmniOutliner (for sorting with respect to a “Date” column)?

Well, there certainly is. Unfortunately, there is no direct way to plug it into OmniOutliner (OO and OF APIs share some classes but there are differences, of course).

OO API has a Column class, and the Item class has an Instance Function named valueForColumn(column: Column). Also, Outline class has an Instance Function named moveItems(items: Array of Item, position: ItemPosition).

Feel free to send me a Direct Message if you are interested.