results from sort-by are not sorted

108 views
Skip to first unread message

sheng.p...@gmail.com

unread,
May 5, 2019, 10:48:18 PM5/5/19
to Clojure
Hey there, 

in my lein repl, 

(sort-by #{:age} [{:age 3,:name "luo"},{:age 1,:name "sheng"}]) 

returns 

({:age 3, :name "luo"} {:age 1, :name "sheng"}) 

rather than 

({:age 1, :name "sheng"}, {:age 3, :name "luo"}). 

Why?

Bill Xie

unread,
May 5, 2019, 10:55:52 PM5/5/19
to clo...@googlegroups.com
clojure.core/sort-by
([keyfn coll] [keyfn comp coll])
  Returns a sorted sequence of the items in coll, where the sort
  order is determined by comparing (keyfn item).  If no comparator is
  supplied, uses compare.  comparator must implement
  java.util.Comparator.  Guaranteed to be stable: equal elements will
  not be reordered.  If coll is a Java array, it will be modified.  To
  avoid this, sort a copy of the array.
nil

<sheng.p...@gmail.com> 于2019年5月6日周一 上午10:48写道:
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
/**
* Bill Xie
* twitter: @joyrap
*/

se...@corfield.org

unread,
May 5, 2019, 11:34:36 PM5/5/19
to clo...@googlegroups.com

(sort-by #{:age} …) will use the set #{:age} as the keyfn, and both (#{:age} {:age 3, :name “luo”}) and (#{:age} {:age 1, :name “sheng”}) both return :age – because both maps contain the key in the set. As far as sort-by is concerned, both hash maps compare equal.

 

What you want is (sort-by :age …) so you the keyfn pulls the value corresponding to :age out of the hash maps. That will produce 3 from {:age 3, :name “luo”} and 1 from {:age 1, :name “sheng”} so they will sort appropriately.

 

Sean Corfield -- (970) FOR-SEAN -- (904) 302-SEAN
An Architect's View -- http://corfield.org/

"If you're not annoying somebody, you're not really alive."
-- Margaret Atwood

--

sheng.p...@gmail.com

unread,
May 6, 2019, 1:22:21 AM5/6/19
to Clojure
Thanks. What a newbie question.

在 2019年5月6日星期一 UTC+8上午11:34:36,se...@corfield.org写道:

(sort-by #{:age} …) will use the set #{:age} as the keyfn, and both (#{:age} {:age 3, :name “luo”}) and (#{:age} {:age 1, :name “sheng”}) both return :age – because both maps contain the key in the set. As far as sort-by is concerned, both hash maps compare equal.

 

What you want is (sort-by :age …) so you the keyfn pulls the value corresponding to :age out of the hash maps. That will produce 3 from {:age 3, :name “luo”} and 1 from {:age 1, :name “sheng”} so they will sort appropriately.

 

Sean Corfield -- (970) FOR-SEAN -- (904) 302-SEAN
An Architect's View -- http://corfield.org/

"If you're not annoying somebody, you're not really alive."
-- Margaret Atwood

 

From: sheng....@gmail.com
Sent: Sunday, May 5, 2019 7:48 PM
To: Clojure
Subject: results from sort-by are not sorted

 

Hey there, 

 

in my lein repl, 

 

(sort-by #{:age} [{:age 3,:name "luo"},{:age 1,:name "sheng"}]) 

 

returns 

 

({:age 3, :name "luo"} {:age 1, :name "sheng"}) 

 

rather than 

 

({:age 1, :name "sheng"}, {:age 3, :name "luo"}). 

 

Why?

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to


For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.

To unsubscribe from this group and stop receiving emails from it, send an email to clo...@googlegroups.com.

Justin Smith

unread,
May 6, 2019, 4:54:14 PM5/6/19
to Clojure
minor nitpick to the answer Sean provided: #{:age} as a function returns :age for an argument equal to :age and nil for all other inputs, including a hash map containing that key.


For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.

Sean Corfield

unread,
May 7, 2019, 10:30:39 AM5/7/19
to clo...@googlegroups.com

Good catch, thank you! And that was my _second_ edit (my first draft was also wrong in a different way). Lesson: just try this stuff in the REPL to see what _really_ happens! 😊

 

Sean Corfield -- (904) 302-SEAN


An Architect's View -- http://corfield.org/

"If you're not annoying somebody, you're not really alive."
-- Margaret Atwood

 


From: clo...@googlegroups.com <clo...@googlegroups.com> on behalf of Justin Smith <noise...@gmail.com>
Sent: Monday, May 6, 2019 1:53:48 PM
To: Clojure
Subject: Re: results from sort-by are not sorted
 
Reply all
Reply to author
Forward
0 new messages