|
1
|
|
|
// Copyright 2018 Fedir RYKHTIK. All rights reserved. |
|
2
|
|
|
// Use of this source code is governed by the GNU GPL 3.0 |
|
3
|
|
|
// license that can be found in the LICENSE file. |
|
4
|
|
|
|
|
5
|
|
|
package sorting |
|
6
|
|
|
|
|
7
|
|
|
import ( |
|
8
|
|
|
"log" |
|
9
|
|
|
"sort" |
|
10
|
|
|
"strconv" |
|
11
|
|
|
) |
|
12
|
|
|
|
|
13
|
|
|
func SortSliceByColumnIndexIntAsc(s [][]string, columnIndex int) [][]string { |
|
|
|
|
|
|
14
|
|
|
s = sortSliceByColumnIndexInt(s, columnIndex, "asc") |
|
15
|
|
|
return s |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
func SortSliceByColumnIndexIntDesc(s [][]string, columnIndex int) [][]string { |
|
|
|
|
|
|
19
|
|
|
s = sortSliceByColumnIndexInt(s, columnIndex, "desc") |
|
20
|
|
|
return s |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
func sortSliceByColumnIndexInt(s [][]string, columnIndex int, direction string) [][]string { |
|
24
|
|
|
if columnIndex == 0 { |
|
25
|
|
|
log.Fatalf("Error occurred. Please check map of columns indexes") |
|
26
|
|
|
} |
|
27
|
|
|
sort.Slice(s, func(i, j int) bool { |
|
28
|
|
|
firstCellValue, _ := strconv.Atoi(s[i][columnIndex]) |
|
29
|
|
|
secondCellValue, _ := strconv.Atoi(s[j][columnIndex]) |
|
30
|
|
|
if direction == "desc" { |
|
31
|
|
|
return firstCellValue > secondCellValue |
|
32
|
|
|
} |
|
33
|
|
|
return firstCellValue < secondCellValue |
|
34
|
|
|
}) |
|
35
|
|
|
return s |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
func SortSliceByColumnIndexFloatDesc(s [][]string, columnIndex int) [][]string { |
|
|
|
|
|
|
39
|
|
|
if columnIndex == 0 { |
|
40
|
|
|
log.Fatalf("Error occurred. Please check map of columns indexes") |
|
41
|
|
|
} |
|
42
|
|
|
sort.Slice(s, func(i, j int) bool { |
|
43
|
|
|
firstCellValue, _ := strconv.ParseFloat(s[i][columnIndex], 32) |
|
44
|
|
|
secondCellValue, _ := strconv.ParseFloat(s[j][columnIndex], 32) |
|
45
|
|
|
return firstCellValue > secondCellValue |
|
46
|
|
|
}) |
|
47
|
|
|
return s |
|
48
|
|
|
} |
|
49
|
|
|
|