+
+;;;###autoload
+(defun rmail-sort-by-labels (reverse labels)
+ "Sort messages of current Rmail file by labels.
+If prefix argument REVERSE is non-nil, sort them in reverse order.
+KEYWORDS is a comma-separated list of labels."
+ (interactive "P\nsSort by labels: ")
+ (or (string-match "[^ \t]" labels)
+ (error "No labels specified"))
+ (setq labels (concat (substring labels (match-beginning 0)) ","))
+ (let (labelvec)
+ (while (string-match "[ \t]*,[ \t]*" labels)
+ (setq labelvec (cons
+ (concat ", ?\\("
+ (substring labels 0 (match-beginning 0))
+ "\\),")
+ labelvec))
+ (setq labels (substring labels (match-end 0))))
+ (setq labelvec (apply 'vector (nreverse labelvec)))
+ (rmail-sort-messages reverse
+ (function
+ (lambda (msg)
+ (let ((n 0))
+ (while (and (< n (length labelvec))
+ (not (rmail-message-labels-p
+ msg (aref labelvec n))))
+ (setq n (1+ n)))
+ n))))))