I'm new to Racket and I'm trying to define a function sort-mail
that is gonna sort a hash table.
I've some defined lists:
(define test-dates
'("Sun, 10 Sep 2017 09:48:44 +0200"
"Wed, 13 Sep 2017 17:51:05 +0000"
"Sun, 10 Sep 2017 13:16:19 +0200"
"Tue, 17 Nov 2009 18:21:38 -0500"
"Wed, 13 Sep 2017 10:40:47 -0700"
"Thu, 14 Sep 2017 12:03:35 -0700"
"Wed, 18 Nov 2009 02:22:12 -0800"
"Sat, 09 Sep 2017 13:40:18 -0700"
"Tue, 26 Oct 2010 15:11:06 +0200"
"Tue, 17 Nov 2009 18:04:31 -0800"
"Mon, 17 Oct 2011 04:15:12 +0000"
"Sun, 16 Oct 2011 23:12:02 -0500"
"Mon, 11 Sep 2017 14:41:12 +0100"))
(define sorted-dates
'("Tue, 17 Nov 2009 18:04:31 -0800"
"Tue, 17 Nov 2009 18:21:38 -0500"
"Wed, 18 Nov 2009 02:22:12 -0800"
"Tue, 26 Oct 2010 15:11:06 +0200"
"Sun, 16 Oct 2011 23:12:02 -0500"
"Mon, 17 Oct 2011 04:15:12 +0000"
"Sat, 09 Sep 2017 13:40:18 -0700"
"Sun, 10 Sep 2017 09:48:44 +0200"
"Sun, 10 Sep 2017 13:16:19 +0200"
"Mon, 11 Sep 2017 14:41:12 +0100"
"Wed, 13 Sep 2017 10:40:47 -0700"
"Wed, 13 Sep 2017 17:51:05 +0000"
"Thu, 14 Sep 2017 12:03:35 -0700"))
The function is supposed to pass this test.
(module+ test
(define test-hashes (map (lambda (x) (hasheq 'Date x)) test-dates))
(define sorted-hashes (map (lambda (x) (hasheq 'Date x)) sorted-dates))
(check-equal? (sort-mail test-hashes) sorted-hashes))
So, how do I even start? I find hash tables in Racket very difficult. I thought of using the sort
function, but it guess it doesn't take a hash table as an argument.
Hash tables are inherently sorted. By design, they allow for (theoretically) instant lookup time by mapping a unique key to an index. So, there is no sorting mechanism to act on a hash-map as there is no need. If you are trying to aggregate the key value pairs into a list, and then sort, that is certainly possible.
hash-keys will return a list of keys in the table. hash-values will return a list of values in the table.
These lists can be sorted. You can also pair each element of each list together, (so a list of key-value pairs). Try the following: