mirror of
https://codeberg.org/scip/tablizer.git
synced 2025-12-17 04:30:56 +01:00
added support to sort by time, duration, numerical
This commit is contained in:
47
lib/sort.go
47
lib/sort.go
@@ -18,7 +18,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
package lib
|
||||
|
||||
import (
|
||||
"github.com/araddon/dateparse"
|
||||
str2duration "github.com/xhit/go-str2duration/v2"
|
||||
"sort"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func sortTable(data *Tabdata, col int) {
|
||||
@@ -41,9 +44,45 @@ func sortTable(data *Tabdata, col int) {
|
||||
|
||||
// actual sorting
|
||||
sort.SliceStable(data.entries, func(i, j int) bool {
|
||||
if SortDescending {
|
||||
return data.entries[i][col] > data.entries[j][col]
|
||||
}
|
||||
return data.entries[i][col] < data.entries[j][col]
|
||||
return compare(data.entries[i][col], data.entries[j][col])
|
||||
})
|
||||
}
|
||||
|
||||
func compare(a string, b string) bool {
|
||||
var comp bool
|
||||
|
||||
switch {
|
||||
case SortNumeric:
|
||||
left, err := strconv.Atoi(a)
|
||||
if err != nil {
|
||||
left = 0
|
||||
}
|
||||
right, err := strconv.Atoi(b)
|
||||
if err != nil {
|
||||
right = 0
|
||||
}
|
||||
comp = left < right
|
||||
case SortAge:
|
||||
left, err := str2duration.ParseDuration(a)
|
||||
if err != nil {
|
||||
left = 0
|
||||
}
|
||||
right, err := str2duration.ParseDuration(b)
|
||||
if err != nil {
|
||||
right = 0
|
||||
}
|
||||
comp = left.Seconds() < right.Seconds()
|
||||
case SortTime:
|
||||
left, _ := dateparse.ParseAny(a)
|
||||
right, _ := dateparse.ParseAny(b)
|
||||
comp = left.Unix() < right.Unix()
|
||||
default:
|
||||
comp = a < b
|
||||
}
|
||||
|
||||
if SortDescending {
|
||||
comp = !comp
|
||||
}
|
||||
|
||||
return comp
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user