### [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.