On Fri, Jan 9, 2009 at 2:59 AM, Mark Engelberg <mark.engelb...@gmail.com> wrote: > > Lexicographic ordering. Compare the first elements, if equal compare the > rests.
Which is how vectors sort: user=> (sort [[3 2 1] [1 2 3]]) ([1 2 3] [3 2 1]) But vector grow on the right, like English words, so using English-word-like sorting rules makes sense. The first item you put in the vector will remain the most significant -- anything you conj onto it is a detail that will change the order only slightly. I wonder about lists, though. Would the left- or right-most value be "most significant" for the pursposes of sorting? My first thought would be for left-most to be most significant, just like vectors. ...but then it seems odd that using conj would drastically change the value of the list: user=> (conj '(1 2 3) 4) (4 1 2 3) But the other option, making the right-most value the most significant would mean every compare would have to start with a linear-time traversal to the end of the list, and then work backward. Essentially a 'reverse'. So maybe leaving 'sort' undefined for lists makes sense. If you want them to sort like vectors, then use vectors! ; set up our test -- 100,000 lists, each with 10 random ints (defmacro rep [len f] `(take ~len (repeatedly (fn [] ~f)))) (def test-grid (rep 100000 (apply list (rep 10 (rand-int Integer/MAX_VALUE))))) ; convert them all to vectors and sort user=> (time (last (sort (map vec test-grid)))) "Elapsed time: 209.975313 msecs" [2147476709 1551972320 719556655 1690536219 1972256927 1115500269 2123276906 939962205 290318355 882332308] ; or if you really want them each back as a list or seq: user=> (time (last (map seq (sort (map vec test-grid))))) "Elapsed time: 250.105186 msecs" (2147476709 1551972320 719556655 1690536219 1972256927 1115500269 2123276906 939962205 290318355 882332308) user=> (time (last (map #(apply list %) (sort (map vec test-grid))))) "Elapsed time: 383.479598 msecs" (2147476709 1551972320 719556655 1690536219 1972256927 1115500269 2123276906 939962205 290318355 882332308) ; any of those are of course faster than converting each list to a vec ; each time it needs to be compared: user=> (time (last (sort-by vec test-grid))) "Elapsed time: 1744.390581 msecs" (2147476709 1551972320 719556655 1690536219 1972256927 1115500269 2123276906 939962205 290318355 882332308) --Chouser --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~----------~----~----~----~------~----~------~--~---