resolved #170 native driver sort

This commit is contained in:
微凉
2021-11-23 16:09:42 +08:00
parent 163ee1159e
commit 8cfabfd0f5
2 changed files with 52 additions and 3 deletions

View File

@ -1,6 +1,10 @@
package model
import "time"
import (
"sort"
"strings"
"time"
)
type File struct {
Name string `json:"name"`
@ -10,4 +14,35 @@ type File struct {
UpdatedAt *time.Time `json:"updated_at"`
Thumbnail string `json:"thumbnail"`
Url string `json:"url"`
}
}
func SortFiles(files []File, account *Account) {
if account.OrderBy == "" {
return
}
sort.Slice(files, func(i, j int) bool {
switch account.OrderBy {
case "name":
{
c := strings.Compare(files[i].Name, files[j].Name)
if account.OrderDirection == "DESC" {
return c >= 0
}
return c <= 0
}
case "size":
{
if account.OrderDirection == "DESC" {
return files[i].Size >= files[j].Size
}
return files[i].Size <= files[j].Size
}
case "updated_at":
if account.OrderDirection == "DESC" {
return files[i].UpdatedAt.After(*files[j].UpdatedAt)
}
return files[i].UpdatedAt.Before(*files[j].UpdatedAt)
}
return false
})
}