Search code examples
lispclisp

What's the best way to sort a hashtable by value?


Now i have to copy the hastable to a list before sorting it:

(defun good-red ()
  (let ((tab (make-hash-table)) (res '()))
    (dotimes (i 33) (setf (gethash (+ i 1) tab) 0))
    (with-open-file (stream "test.txt")
        (loop for line = (read-line stream nil)
             until (null line)
             do
                (setq nums (butlast (str2lst (substring line 6))))
                (dolist (n nums) (incf (gethash n tab)))
                ))
    **(maphash #'(lambda (k v) (push (cons k v) res)) tab)**
    (setq sort-res (sort res #'< :key #'cdr))
    (reverse (nthcdr (- 33 18) (mapcar #'car sort-res))) ))

BTW, what's the better way to fetch the first N elements of a list ?


Solution

  • Vatine's answer is technically correct, but probably not super helpful for the immediate problem of someone asking this question. The common case of using a hash table to hold a collection of counters, then selecting the top N items by score can be done like this:

    ;; convert the hash table into an association list
    (defun hash-table-alist (table)
      "Returns an association list containing the keys and values of hash table TABLE."
      (let ((alist nil))
        (maphash (lambda (k v)
                   (push (cons k v) alist))
                 table)
        alist))
    
    (defun hash-table-top-n-values (table n)
      "Returns the top N entries from hash table TABLE. Values are expected to be numeric."
      (subseq (sort (hash-table-alist table) #'> :key #'cdr) 0 n))
    

    The first function returns the contents of a hash table as a series of cons'd pairs in a list, which is called an association list (the typical list representation for key/value pairs). Most Lisp enthusiasts already have a variation of this function on hand because it's such a common operation. This version is from the Alexandria library, which is very widely used in the CL community.

    The second function uses SUBSEQ to grab the first N items from the list returned by sorting the alist returned by the first function using the CDR of each pair as the key. Changing :key to #'car would sort by hash keys, changing #'> to #'< would invert the sort order.