-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnotes.txt
62 lines (42 loc) · 1.83 KB
/
notes.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
:project wevre/natural-compare
:author Mike Weaver
:created 2020-07
:section Introduction
A comparator for natural sorting of strings. Embedded digits are compared as
integers. Inspired by Wilker Lúcio's {`natural-sort.clj` gist}[wilkerlucio].
:link wilkerlucio
https://gist.github.com/wilkerlucio/db54dc83a9664124f3febf6356f04509
:section How to use
When comparing strings, use natural-compare as the comparator.
``` deps.edn
{...
:deps {...
wevre/natural-compare {:git/url ""
:sha ""}
...}
...}
``` your-namespace.clj
(ns your-namespace
(:require [wevre.natural-compare :refer [natural-compare]]))
(def ss ["t3" "t1" "t10" "t12" "t2" "t27"])
(sort natural-compare arr)
;; => ("t1" "t2" "t3" "t10" "t12" "t27")
:section How it works
`natural-compare` compares two strings. Each is split into a sequence of
alternating text and integer elements and then the two sequences are compared
element by element. The trick is to make sure that when splitting the string,
the first element is always text, the elements always alternate between text
and integer, and the last element is an integer. The sequence is padded as
necessary with values "" and -1 (which always sort lower than 'legit' values)
to make sure shorter strings sort first.
See test cases for examples of splitting.
:section Learnings
* Test files need to be `.clj` for test-runner to find them.
* Reader conditionals only work in `.cljc` files.
* POM file needs a <resources> section under the <build> section in order to
include the .clj files in the output JAR file.
:section Deploying
I installed
```
env CLOJARS_USERNAME=username CLOJARS_PASSWORD=clojars-token \
clojure -T:build deploy