### [CLJ-1382] Vector sort order should be specified sufficiently to embrace sort-by-juxt Created: 13/Mar/14  Updated: 15/Mar/14  Resolved: 15/Mar/14

Status: Closed
Project: Clojure
Component/s: None
Affects Version/s: Release 1.5
Fix Version/s: None

 Type: Enhancement Priority: Minor Reporter: Phill Wolf Assignee: Unassigned Resolution: Completed Votes: 0 Labels: data-structures, documentation, idiom

 Description
 Vectors of equal length sort in a way that seems natural – by comparing their 0th elements, then their 1st, etc., until something is different: ```user> (def vv '(["c" 9] ["a" 100] ["a" 33] ["b" 8])) #'user/vv user> (sort vv) (["a" 33] ["a" 100] ["b" 8] ["c" 9])``` This property enables a blisteringly wonderful idiom for sorting records by multiple keys: ```user> (def mm (->> vv (map (fn [[p q]] {:p p :q q})))) #'user/mm user> (sort-by (juxt :p :q) mm) ({:p "a", :q 33} {:p "a", :q 100} {:p "b", :q 8} {:p "c", :q 9})``` The sort-by-juxt idiom was described on briancarper.net[1], where it was refined for Clojure 1.1 by Malcolm Sparks. Andy Fingerhut has also written about it, thoroughly.[2] Such lore gives it the odor of respectability, but the sort-by-juxt idiom takes liberties beyond the documented behavior ("contract") of vectors. APersistentVector indulges the idiom, but the clojure.org Data Structures page does not say how vectors should compare. The vector specification should be bolstered with enough traits of vectors' sorting behavior to make the sort-by-juxt idiom safe to use wherever Clojure might be implemented.

 Comment by Alex Miller [ 13/Mar/14 9:52 PM ] I don't understand what this ticket is asking for. Comment by Andy Fingerhut [ 13/Mar/14 10:32 PM ] It sounds like he is asking that the doc of clojure.core/compare say that vectors of equal length are compared in lexicographic order. Comment by Phill Wolf [ 15/Mar/14 1:07 PM ] "(sort-by (juxt" relies on a feature of vectors that the Clojure documentation does not guarantee. But using juxt in this way is part of the cultural fabric and helps make concise programs that "read like a definition" of the problem, to quote Halloway in "Programming Clojure". Therefore, let's document the sort order of equal-length vectors. I looked for this information on the Data Structures page, which has a section on vectors. Comment by Alex Miller [ 15/Mar/14 11:11 PM ] I added a line to the Vectors section on the Data Structures (http://clojure.org/data_structures) page: "Vectors are compared first by length, then each element is compared in order."

Generated at Fri Oct 09 05:44:08 CDT 2015 using JIRA 4.4#649-r158309.