-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsortBy.bat
executable file
·52 lines (43 loc) · 1.19 KB
/
sortBy.bat
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
::#! 2>/dev/null || echo "
@echo off
call scala -savecompiled %~f0 %*
goto :eof
" >//null
#!/bin/sh
exec scala -savecompiled "$0" "$@"
::!#
val method = args match {
case Array(regex) => None
case Array(regex, "num") => Some("num")
case _ => {
println("""
| Sort lines based on a group in a given regex.
| If an optional 'num' parameter is given, sorting assumes the sort key is numeric.
| Otherwise the sorting is based on natural order of strings.
| Usage:
| sortBy "<regex>"
| sortBy "<regex>" num
| Examples:
| (echo a1 && echo b3 && echo c22) | sortBy "[a-z](\d*).*"
| a1
| c22
| b3
| (echo a1 && echo b3 && echo c22) | sortBy "[a-z](\d*).*" num
| a1
| b3
| c22
""".stripMargin)
exit
}
}
import scala.io._
val lines = Source.stdin.getLines
val SortPattern = args(0).r
implicit val ordering = (method match {
case Some("num") => implicitly[Ordering[Double]]
case None => implicitly[Ordering[String]]
}).asInstanceOf[Ordering[Any]]
lines.toSeq.sortBy { case SortPattern(x) => method match {
case Some("num") => x.toDouble
case None => x
}} foreach println