Sort a list of object identifiers: Difference between revisions

(Added AppleScript.)
Line 454:
1.3.6.1.4.1.11150.3.4.0
1.3.6.1.4.1.11150.3.4.0.1
</pre>
 
 
=={{header|Clojure}}==
Clojure 'sort' function allows specifying an optional comparator function. In this case, our custom comparator utilizes the ability of the clojure.core 'compare' function to compare vectors in an appropriate fashion.
 
<lang Clojure>
(defn oid-vec [oid-str]
(->> (clojure.string/split oid-str #"\.")
(map #(Long. %))))
 
(defn oid-str [oid-vec]
(clojure.string/join "." oid-vec))
 
;;
;; If vals differ before shorter vec ends,
;; use comparison of that "common header".
;; If common part matches, compare based on num elements.
;;
(defn oid-compare [a b]
(let [min-len (min (count a) (count b))
common-cmp (compare (vec (take min-len a))
(vec (take min-len b)))]
(if (zero? common-cmp)
(compare (count a) (count b))
common-cmp)))
 
(defn sort-oids [oid-strs]
(->> (map oid-vec oid-strs)
(sort oid-compare)
(map oid-str)))
</lang>
 
{{out}}
<pre>
(sort-oids ["1.3.6.1.4.1.11.2.17.19.3.4.0.10"
"1.3.6.1.4.1.11.2.17.5.2.0.79"
"1.3.6.1.4.1.11.2.17.19.3.4.0.4"
"1.3.6.1.4.1.11150.3.4.0.1"
"1.3.6.1.4.1.11.2.17.19.3.4.0.1"
"1.3.6.1.4.1.11150.3.4.0"])
 
("1.3.6.1.4.1.11.2.17.5.2.0.79"
"1.3.6.1.4.1.11.2.17.19.3.4.0.1"
"1.3.6.1.4.1.11.2.17.19.3.4.0.4"
"1.3.6.1.4.1.11.2.17.19.3.4.0.10"
"1.3.6.1.4.1.11150.3.4.0"
"1.3.6.1.4.1.11150.3.4.0.1")
</pre>
 
Anonymous user