-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort.wok
39 lines (34 loc) · 1.46 KB
/
sort.wok
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
#!/usr/bin/wok -f
/** sort.wok
*
* Usage:
* java -jar wok-0.1.0.jar -f sort.wok -v idx=0 -v@str format=str -v@str order=asc input > output
*
* Option:
* -v idx: The index number of the reference column to sort rows. This value must be larger than or equal to zero.
* -v@str format: This value specifies the data type of cells of the column specified by `idx`.
* Possible options are `str`, `int` and `float`.
* -v@str order: This value specifies the sorting order of the result. Possible options are `asc` and `desc`.
*/
FS = '\t'
FQ = Quote.Min
OFS = '\t'
OFQ = Quote.All
val asc = order.toLowerCase match {
case "a"| "asc" => true
case "d"| "desc" => false
}
val sorter = format.toLowerCase match {
case "s" | "str" | "string" =>
if (asc) (a: List[String], b: List[String]) => a(idx).toLowerCase < b(idx).toLowerCase
else (a: List[String], b: List[String]) => a(idx).toLowerCase > b(idx).toLowerCase
case "i" | "int" | "long" =>
if (asc) (a: List[String], b: List[String]) => a(idx).toLong < b(idx).toLong
else (a: List[String], b: List[String]) => a(idx).toLong > b(idx).toLong
case "f" | "float" | "double" =>
if (asc) (a: List[String], b: List[String]) => a(idx).toDouble < b(idx).toDouble
else (a: List[String], b: List[String]) => a(idx).toDouble > b(idx).toDouble
}
In { _.toList }
.sortWith(sorter)
.foreach (row => println(row: _*))