-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path3.custom-sort-strings-solution.go
More file actions
70 lines (56 loc) · 1.13 KB
/
3.custom-sort-strings-solution.go
File metadata and controls
70 lines (56 loc) · 1.13 KB
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
63
64
65
66
67
68
69
70
package main
import (
"bufio"
"fmt"
"os"
"reflect"
"sort"
"strconv"
"strings"
)
func getStringAtPosK(s string, k int) string {
return strings.Split(s, " ")[k-1]
}
func main() {
in := bufio.NewReader(os.Stdin)
var n int
_, _ = fmt.Scan(&n)
strs := make([]string, 0)
for i := 0; i < n; i++ {
s, _ := in.ReadString('\n')
strs = append(strs, s)
}
var pos int
var reversal bool
var cmpType string
_, _ = fmt.Scanln(&pos, &reversal, &cmpType)
keyList := make([][2]string, 0)
for i := 0; i < n; i++ {
key := getStringAtPosK(strs[i], pos)
keyList = append(keyList, [2]string{
strs[i], key,
})
}
if cmpType == "lexicographic" {
sort.Slice(keyList, func(i, j int) bool {
return keyList[i][1] < keyList[j][1]
})
} else {
sort.Slice(keyList, func(i, j int) bool {
first, _ := strconv.Atoi(keyList[i][1])
second, _ := strconv.Atoi(keyList[j][1])
return first < second
})
}
if reversal {
swap := reflect.Swapper(keyList)
for i, j := 0, n-1; i < j; i, j = i+1, j-1 {
swap(i, j)
}
}
fmt.Println("\nThe final list is:-")
for _, v := range keyList {
fmt.Print(v[0])
}
fmt.Println()
}