Files
HackBrowserData/internal/export/order.go
T
Roger 3e9150249c refactor: rename core types and spell out every name (#677)
Def, Request, DirID, and Source said too little, and short variables and doc comments that skip their name hid intent.
varnamelen and staticcheck ST1020-22 now keep both from coming back.
2026-11-02 11:06:05 +08:00

45 lines
1.0 KiB
Go

package export
import (
"sort"
"time"
"github.com/moond4rk/hackbrowserdata/internal/record"
)
// sortRecords is stable, so ties and keyless records keep source order: Safari bookmarks and downloads lack a time.
func sortRecords(records []record.Record) {
sort.SliceStable(records, func(i, j int) bool {
return keyOf(records[i]).before(keyOf(records[j]))
})
}
type sortKey struct {
visits int
timestamp time.Time
}
func keyOf(row record.Record) sortKey {
switch typed := row.(type) {
case *record.Login:
return sortKey{timestamp: typed.CreatedAt}
case *record.Cookie:
return sortKey{timestamp: typed.CreatedAt}
case *record.Bookmark:
return sortKey{timestamp: typed.CreatedAt}
case *record.Download:
return sortKey{timestamp: typed.StartTime}
case *record.History:
return sortKey{visits: typed.VisitCount}
default:
return sortKey{}
}
}
func (k sortKey) before(other sortKey) bool {
if k.visits != other.visits {
return k.visits > other.visits
}
return k.timestamp.After(other.timestamp)
}