# Sorting todos by org-roam file creation date prefix

**URL:** <https://org-roam.discourse.group/t/sorting-todos-by-org-roam-file-creation-date-prefix/1667>\
**Category:** How To\
**Created:** [June 6, 2021, 8:37pm UTC](https://org-roam.discourse.group/t/sorting-todos-by-org-roam-file-creation-date-prefix/1667 "2021-06-06T20:37:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![gcoladon](https://yyz2.discourse-cdn.com/free1/user_avatar/org-roam.discourse.group/gcoladon/32/976_2.png) [@gcoladon](https://org-roam.discourse.group/u/gcoladon)\
**Post date:** [June 6, 2021, 8:37pm UTC](https://org-roam.discourse.group/t/sorting-todos-by-org-roam-file-creation-date-prefix/1667/1 "2021-06-06T20:37:13Z")

</div>

I have no idea how useful this would be to others, but in case it is, I thought I would share it. And I’m also interested in hearing how others would have accomplished the same result with a better method.

My org-roam files have a “YYMMDD\_” prefix. When I used to run `org-todo-list`, the todos were showing up in a pretty random order as far as I could tell. I decided I wanted to have them in descending order of the date prefix of the file they appear in. When I googled for this, I found [one person’s approach](https://ag91.github.io/blog/2020/09/27/org-agenda-and-your-future-or-how-to-keep-score-of-your-long-term-goals-with-org-mode/) (under `my/org-agenda-sort-longterm-tags`). I modified that slightly to arrive at:

```lisp
;; this function sorts todos by the yymmdd timestamp in the roam filename
;; so the todos in the most recently created files are at the top.
(defun <=> (a b)
  "Compare A and B, returning 1, 0, or -1"
  (cond ((= a b) 0)
        ((> a b) 1)
        ((< a b) -1)))

(defun gpc/org-agenda-cmp-user-defined (a b)
  "Compare two org-agenda-events by their org-roam file prefix datestamp"
  (let* ((regex "\\(2[0-9][0-9][0-9][0-9][0-9]\\)")
         (date-a (if (string-match regex a)
                     (string-to-number (or (match-string 1 a) "0"))
                   0))
         (date-b (if (string-match regex b)
                     (string-to-number (or (match-string 1 b) "0"))
                   0)))
    (<=> date-a date-b)))

(setq org-agenda-cmp-user-defined 'gpc/org-agenda-cmp-user-defined)

```

What do people think of this a) desired outcome b) approach for achieving it? I personally assume the code above could be more elegant, but I’m not sure how I would do that.

---

<div class="post-metadata">

**Author:** ![nobiot](https://yyz2.discourse-cdn.com/free1/user_avatar/org-roam.discourse.group/nobiot/32/159_2.png) [@nobiot](https://org-roam.discourse.group/u/nobiot)\
**Post date:** [June 7, 2021, 9:48am UTC](https://org-roam.discourse.group/t/sorting-todos-by-org-roam-file-creation-date-prefix/1667/2 "2021-06-07T09:48:22Z")

</div>

@gcoladon  
I feel that if the approach works for you and you like it, it’s a good one 🙂

Without understanding what output values are really required for sorting todos, if I were to do something like that, I would try to use `org-time*` built-in macros like this.

For the timestamp prefix you have, you would need to covert it to a standard ISO-like format (`my/convert-date`).

Then it would be a matter of using some combinations of `org-time*` macros (in the `let` part). This does not return -1, 0, 1 like your approach does, so it may not work for your purpose.

```auto
  (defun my/convert-date (prefix)
    (concat
     "20"
     (substring prefix 0 2)
     "-"
     (substring prefix 2 4)
     "-"
     (substring prefix 4 6)))
  
  ;; Do something like this in a comparison function
  (let ((date1 "210605")
        (date2 "210606"))
    (org-time<= (my/convert-date date1)
                (my/convert-date date2)))  

```

If you want to sort some sequence (like a list), then I’d see if I could use `seq-sort-by`, etc. like: `(seq-sort-by #'my/convert-date #'org-time<= list)` – this is probably not what you can use for your purpose…
