Subject: Re: bug#53755: 27.2; C-u M-x gnus-summary-sort-by-most-recent-number / -date not ordering threads as expected

From: Michael Welsh Duggan <[email-masked]>
Date: Thu, 03 Feb 2022 22:33:00 -0500

Kévin Le Gouguec <[email-masked]> writes:

> Michael Welsh Duggan <[email-masked]> writes:
>
>> This may be related to Bug #18156.
>>
>> https://debbugs.gnu.org/cgi/bugreport.cgi?bug=18156
>>
>> I have patched my local gnus to allow sorting threads by newest message.
>
> Quoting your use-case:
>
>>                                                                     Here
>> is my use case:  I want to sort the threads in my summary buffer,
>> post-gathering, by the date of the most recent article in the thread.
>> If threads (not the articles in threads) are sorted before they are
>> gathered, this seems less than possible.
>
> I think I filed more or less the same bug (bug#42334), with more or less
> the same conclusion?
>
> Lars suggested a solution before wontfixing it, but I never got around
> to implementing it.  How does your local patch work?

I run with the included patch.  In the group for which I care about
this, I set the following group parameter:

  (gnus-thread-sort-functions
     'md5i-thread-sort-by-most-recent-date-reverse)

where `md5i-thread-sort-by-most-recent-date-reverse' is defined thusly:

    (defun md5i-thread-sort-by-most-recent-date-reverse (h1 h2)
      (<= (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))

Patch follows:

Author:     Michael Welsh Duggan <[email-masked]>
AuthorDate: Thu Jan 29 10:07:35 2015 -0500

Sort threads after gathering them.

Gnus sorts threads, and then gathers subjects into threads.  This can
cause threads to be sorted improperly.

1 file changed, 11 insertions(+), 3 deletions(-)
lisp/gnus/gnus-sum.el | 14 +++++++++++---

modified   lisp/gnus/gnus-sum.el
@@ -4233,8 +4233,8 @@ gnus-summary-prepare
       (gnus-summary-prepare-threads
        (if gnus-show-threads
 	   (gnus-sort-gathered-threads
-	    (funcall gnus-summary-thread-gathering-function
-		     (gnus-sort-threads
+            (gnus-sort-threads
+             (funcall gnus-summary-thread-gathering-function
 		      (gnus-cut-threads (gnus-make-threads)))))
 	 ;; Unthreaded display.
 	 (gnus-sort-articles gnus-newsgroup-headers))))
@@ -4932,6 +4932,12 @@ gnus-remove-thread-1
 	      (1+ (point-at-eol))
 	    (gnus-delete-line)))))))
 
+(defun gnus-make-threaded-sort (func)
+  (gnus-byte-compile
+   `(lambda (t1 t2)
+      (,func (if (stringp (car t1)) (cdr t1) t1)
+             (if (stringp (car t2)) (cdr t2) t2)))))
+
 (defun gnus-sort-threads-recursive (threads func)
   ;; Responsible for sorting the root articles of threads.
   (let ((subthread-sort-func (if (eq gnus-subthread-sort-functions
@@ -4980,7 +4986,9 @@ gnus-sort-threads
     (prog1
 	(condition-case nil
 	    (let ((max-lisp-eval-depth (max max-lisp-eval-depth 5000))
-		  (sort-func (gnus-make-sort-function gnus-thread-sort-functions)))
+		  (sort-func
+                   (gnus-make-threaded-sort
+                    (gnus-make-sort-function gnus-thread-sort-functions))))
 	      (gnus-sort-threads-recursive threads sort-func))
 	  ;; Even after binding max-lisp-eval-depth, the recursive
 	  ;; sorter might fail for very long threads.  In that case,

-- 
Michael Welsh Duggan
([email-masked])