emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Useful utility function: org-sort-multi
@ 2009-08-29  1:38 Ryan C. Thompson
  2009-08-30  8:09 ` Carsten Dominik
  0 siblings, 1 reply; 5+ messages in thread
From: Ryan C. Thompson @ 2009-08-29  1:38 UTC (permalink / raw)
  To: org-mode Mailinglist

I found myself having to sort by multiple criteria, and I was doing it 
with multiple calls to org-sort-entries-or-items. Then I decided to 
abstract the repetition into a function. Here it is:

(defun org-sort-multi (&rest sort-types)
   "Sort successively by a list of criteria, in descending order of 
importance.
For example, sort first by TODO status, then by priority, then by date, 
then alphabetically, case-sensitive.
Each criterion is either a character or a cons pair (BOOL . CHAR), where 
BOOL is whether or not to sort case-sensitively, and CHAR is one of the 
characters defined in ``org-sort-entries-or-items''.
So, the example above could be accomplished with:
  (org-sort-multi ?o ?p ?t (t . ?a))"
   (interactive)
   (mapc #'(lambda (sort-type)
             (when (characterp sort-type) (setq sort-type (cons nil 
sort-type)))
             (org-sort-entries-or-items (car sort-type) (cdr sort-type)))
         (reverse sort-types)))

Note the call to reverse. This makes it so that the first criterion you 
provide is the dominant criterion. Try it out to see how it works, and 
let me know if there's a better way to pass the arguments.

Just as an example, the particular sorting function I wanted to write 
now becomes this:

(defun org-sort-custom ()
   "Sort children of node by todo status and by priority and by date, so 
the * TODO [#A] items with latest dates go to the top."
   (interactive)
   (org-sort-multi ?o ?p ?T))

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Useful utility function: org-sort-multi
  2009-08-29  1:38 Useful utility function: org-sort-multi Ryan C. Thompson
@ 2009-08-30  8:09 ` Carsten Dominik
  2009-08-30  8:33   ` Benjamin Andresen
  0 siblings, 1 reply; 5+ messages in thread
From: Carsten Dominik @ 2009-08-30  8:09 UTC (permalink / raw)
  To: Ryan C. Thompson; +Cc: org-mode Mailinglist


On Aug 29, 2009, at 3:38 AM, Ryan C. Thompson wrote:

> I found myself having to sort by multiple criteria, and I was doing  
> it with multiple calls to org-sort-entries-or-items. Then I decided  
> to abstract the repetition into a function. Here it is:
>
> (defun org-sort-multi (&rest sort-types)
>  "Sort successively by a list of criteria, in descending order of  
> importance.
> For example, sort first by TODO status, then by priority, then by  
> date, then alphabetically, case-sensitive.
> Each criterion is either a character or a cons pair (BOOL . CHAR),  
> where BOOL is whether or not to sort case-sensitively, and CHAR is  
> one of the characters defined in ``org-sort-entries-or-items''.
> So, the example above could be accomplished with:
> (org-sort-multi ?o ?p ?t (t . ?a))"
>  (interactive)
>  (mapc #'(lambda (sort-type)
>            (when (characterp sort-type) (setq sort-type (cons nil  
> sort-type)))
>            (org-sort-entries-or-items (car sort-type) (cdr sort- 
> type)))
>        (reverse sort-types)))
>
> Note the call to reverse. This makes it so that the first criterion  
> you provide is the dominant criterion. Try it out to see how it  
> works, and let me know if there's a better way to pass the arguments.
>
> Just as an example, the particular sorting function I wanted to  
> write now becomes this:
>
> (defun org-sort-custom ()
>  "Sort children of node by todo status and by priority and by date,  
> so the * TODO [#A] items with latest dates go to the top."
>  (interactive)
>  (org-sort-multi ?o ?p ?T))


Hi Ryan,

this looks interesting, but I am not sure I understand how it works.
It looks to me that each sorting step will completely re-sort the entire
list of items, so the final sorting will win in the end.

Or am I missing something here?

- Carsten


>
>
> _______________________________________________
> Emacs-orgmode mailing list
> Remember: use `Reply All' to send replies to the list.
> Emacs-orgmode@gnu.org
> http://lists.gnu.org/mailman/listinfo/emacs-orgmode

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Useful utility function: org-sort-multi
  2009-08-30  8:09 ` Carsten Dominik
@ 2009-08-30  8:33   ` Benjamin Andresen
  2009-08-30 20:55     ` Ryan C. Thompson
  0 siblings, 1 reply; 5+ messages in thread
From: Benjamin Andresen @ 2009-08-30  8:33 UTC (permalink / raw)
  To: org-mode Mailinglist

Hey Carsten,

Carsten Dominik <carsten.dominik@gmail.com> writes:

> On Aug 29, 2009, at 3:38 AM, Ryan C. Thompson wrote:
>
> Hi Ryan,
>
> this looks interesting, but I am not sure I understand how it works.
> It looks to me that each sorting step will completely re-sort the entire
> list of items, so the final sorting will win in the end.
>
> Or am I missing something here?

if you have the following list
* Test Sorting
** TODO Charlie
** WAITING Beta
** TODO Alpha
** STARTED Beta
** STARTED Charlie
** TODO Beta
** STARTED Alpha
** WAITING Charlie
** WAITING Alpha

calling org-multi-sort with ?o ?a will sort it like this
* Test Sorting
** TODO Alpha
** TODO Beta
** TODO Charlie
** STARTED Alpha
** STARTED Beta
** STARTED Charlie
** WAITING Alpha
** WAITING Beta
** WAITING Charlie

but just ?a would completely ignore the TODO, STARTED, WAITING order.

Thanks Ryan, pretty useful.

> - Carsten

br,
benny

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Useful utility function: org-sort-multi
  2009-08-30  8:33   ` Benjamin Andresen
@ 2009-08-30 20:55     ` Ryan C. Thompson
  2009-08-31  5:40       ` Carsten Dominik
  0 siblings, 1 reply; 5+ messages in thread
From: Ryan C. Thompson @ 2009-08-30 20:55 UTC (permalink / raw)
  Cc: org-mode Mailinglist

Benjamin Andresen wrote:

> if you have the following list
> * Test Sorting
> ** TODO Charlie
> ** WAITING Beta
> ** TODO Alpha
> ** STARTED Beta
> ** STARTED Charlie
> ** TODO Beta
> ** STARTED Alpha
> ** WAITING Charlie
> ** WAITING Alpha

> calling org-multi-sort with ?o ?a will sort it like this
> * Test Sorting
> ** TODO Alpha
> ** TODO Beta
> ** TODO Charlie
> ** STARTED Alpha
> ** STARTED Beta
> ** STARTED Charlie
> ** WAITING Alpha
> ** WAITING Beta
> ** WAITING Charlie

> but just ?a would completely ignore the TODO, STARTED, WAITING order.

> Thanks Ryan, pretty useful.


> br,
> benny


> _______________________________________________
> Emacs-orgmode mailing list
> Remember: use `Reply All' to send replies to the list.
> Emacs-orgmode@gnu.org
> http://lists.gnu.org/mailman/listinfo/emacs-orgmode

That's right, the function relies on the fact that org's sorting is 
stable. So the results of earlier sorts are preserved as much as 
possible in later sorts. Of course, this is really inefficient, but oh well.

As another test case, try using (org-sort-multi ?o ?p) on this:
* Multi-sort test
** DONE [#B]
** TODO [#C]
** STARTED [#C]
** STARTED [#A]
** DONE [#C]
** TODO [#B]
** TODO [#A]
** STARTED [#B]
** DONE [#A]


Anyway, Carsten, if you think this would be useful, feel free to include 
some variant of this in org-mode itself. You'd probably want to 
implement it as a one-pass sort in which the set of sorting criteria 
function as a series of fallbacks for tiebreakers, rather than a series 
of sorts.

Ryan

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Re: Useful utility function: org-sort-multi
  2009-08-30 20:55     ` Ryan C. Thompson
@ 2009-08-31  5:40       ` Carsten Dominik
  0 siblings, 0 replies; 5+ messages in thread
From: Carsten Dominik @ 2009-08-31  5:40 UTC (permalink / raw)
  To: Ryan C. Thompson; +Cc: emacs-orgmode

Hi Ryan, hi Benjamin


Yes, I think I was mistaken, I firth though this would not work  
correctly
with more that 2 criteria, but I think now that it does in fact work.

While I think about the if and how to add this, you might just want to
go ahead and add it to the org-hacks.org file on Worg.....

- Carsten

On Aug 30, 2009, at 10:55 PM, Ryan C. Thompson wrote:

> Benjamin Andresen wrote:
>
>> if you have the following list
>> * Test Sorting
>> ** TODO Charlie
>> ** WAITING Beta
>> ** TODO Alpha
>> ** STARTED Beta
>> ** STARTED Charlie
>> ** TODO Beta
>> ** STARTED Alpha
>> ** WAITING Charlie
>> ** WAITING Alpha
>
>> calling org-multi-sort with ?o ?a will sort it like this
>> * Test Sorting
>> ** TODO Alpha
>> ** TODO Beta
>> ** TODO Charlie
>> ** STARTED Alpha
>> ** STARTED Beta
>> ** STARTED Charlie
>> ** WAITING Alpha
>> ** WAITING Beta
>> ** WAITING Charlie
>
>> but just ?a would completely ignore the TODO, STARTED, WAITING order.
>
>> Thanks Ryan, pretty useful.
>
>
>> br,
>> benny
>
>
>> _______________________________________________
>> Emacs-orgmode mailing list
>> Remember: use `Reply All' to send replies to the list.
>> Emacs-orgmode@gnu.org
>> http://lists.gnu.org/mailman/listinfo/emacs-orgmode
>
> That's right, the function relies on the fact that org's sorting is  
> stable. So the results of earlier sorts are preserved as much as  
> possible in later sorts. Of course, this is really inefficient, but  
> oh well.
>
> As another test case, try using (org-sort-multi ?o ?p) on this:
> * Multi-sort test
> ** DONE [#B]
> ** TODO [#C]
> ** STARTED [#C]
> ** STARTED [#A]
> ** DONE [#C]
> ** TODO [#B]
> ** TODO [#A]
> ** STARTED [#B]
> ** DONE [#A]
>
>
> Anyway, Carsten, if you think this would be useful, feel free to  
> include some variant of this in org-mode itself. You'd probably want  
> to implement it as a one-pass sort in which the set of sorting  
> criteria function as a series of fallbacks for tiebreakers, rather  
> than a series of sorts.
>
> Ryan
>
>
>
> _______________________________________________
> Emacs-orgmode mailing list
> Remember: use `Reply All' to send replies to the list.
> Emacs-orgmode@gnu.org
> http://lists.gnu.org/mailman/listinfo/emacs-orgmode

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2009-08-31  6:50 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-08-29  1:38 Useful utility function: org-sort-multi Ryan C. Thompson
2009-08-30  8:09 ` Carsten Dominik
2009-08-30  8:33   ` Benjamin Andresen
2009-08-30 20:55     ` Ryan C. Thompson
2009-08-31  5:40       ` Carsten Dominik

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).