Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sorting issue #2 #554

Closed
jkitchin opened this issue Jun 12, 2016 · 3 comments
Closed

Sorting issue #2 #554

jkitchin opened this issue Jun 12, 2016 · 3 comments

Comments

@jkitchin
Copy link
Contributor

@jkitchin jkitchin commented Jun 12, 2016

Say I have some candidates as a list of cons cells, and I want them sorted on the cdr of the cell. Is this the right way to do it?

#+BEGIN_SRC emacs-lisp
(defvar ivy-sorter-data '(("b 1" . 1) ("a 2" . 2) ("d 0" . 0) ("c 5" . 5)))

(defun isn (a b)
  (< (cdr (assoc a ivy-sorter-data)) (cdr (assoc b ivy-sorter-data))))

(add-to-list 'ivy-sort-functions-alist '(ivy-sorter . isn))

(defun ivy-sorter (&rest args) (mapcar 'car ivy-sorter-data))

(ivy-read "string: " 'ivy-sorter
      :sort t)
#+END_SRC
@abo-abo
Copy link
Owner

@abo-abo abo-abo commented Jun 13, 2016

Yes, this is a correct approach. Looking at it now, it is quite a bit clunky. I'm adding a commit to make this work:

(defvar ivy-sorter-data '(("b 1" . 1) ("a 2" . 2) ("d 0" . 0) ("c 5" . 5)))

(defun isn (a b)
  (< (cdr a) (cdr b)))

(add-to-list 'ivy-sort-functions-alist '(ivy-sorter . isn))

(ivy-read "string: " ivy-sorter-data
          :sort t
          :caller 'ivy-sorter)
abo-abo added a commit that referenced this issue Jun 13, 2016
* ivy.el (ivy--reset-state): When an alist is passed to `ivy-read', and
  a sorting function is associated via 'caller, the sorting function
  will receive two cons cells as arguments, instead of two strings as
  usual.

Re #554

Example:

(defvar ivy-sorter-data '(("b 1" . 1) ("a 2" . 2) ("d 0" . 0) ("c 5" . 5)))

(defun isn (a b)
  (< (cdr a) (cdr b)))

(add-to-list 'ivy-sort-functions-alist '(ivy-sorter . isn))

(ivy-read "string: " ivy-sorter-data
          :sort t
          :caller 'ivy-sorter)
@jkitchin
Copy link
Contributor Author

@jkitchin jkitchin commented Jun 13, 2016

That is nice, thanks! That makes things like this possible too.

(defun isn (a b) 
  (string< (plist-get (cdr a) :file) (plist-get (cdr b) :file)))

(add-to-list 'ivy-sort-functions-alist '(ivy-sorter . isn))

(ivy-read "string: " '(("a" :key 1 :file "h.el")
               ("b" :key 2 :file "g.el"))
      :sort t
      :caller 'ivy-sorter)

and

(defun isn (a b) 
  (> (elt a 2) (elt b 2)))

(add-to-list 'ivy-sort-functions-alist '(ivy-sorter . isn))

(ivy-read "string: " '(("a" 1 2)
               ("b" 2 1))
      :sort t
      :caller 'ivy-sorter)

@abo-abo
Copy link
Owner

@abo-abo abo-abo commented Jun 13, 2016

This one looks resolved, closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants