Created
January 23, 2019 12:59
-
-
Save abonec/f1ee23a38e78ea48d470c39885de47ba to your computer and use it in GitHub Desktop.
sortable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"sort" | |
"time" | |
) | |
type Sortable interface { | |
Copy() Sortable | |
GetSorter(string) sort.Interface | |
Supported() []string | |
} | |
func sortStats(srt Sortable, fieldName, direction string) (Sortable, error) { | |
if fieldName == "" { | |
return srt, nil | |
} | |
copied := srt.Copy() | |
sorter := srt.GetSorter(fieldName) | |
if sorter == nil { | |
return nil, fmt.Errorf("sorting by %s is not supported, only: %v", fieldName, copied.Supported()) | |
} | |
if direction == "desc" { | |
sorter = sort.Reverse(sorter) | |
} | |
sort.Sort(sorter) | |
return copied, nil | |
} | |
type Interline struct { | |
Departure time.Time `json:"departure"` | |
Arrival time.Time `json:"arrival"` | |
DurationString string `json:"duration_string"` | |
Duration time.Duration `json:"duration"` | |
TransferTypes []string `json:"transfer_types"` | |
Directs []Interline `json:"directs"` | |
} | |
type interlinesSortable []Interline | |
type interlineSortDuration []Interline | |
type interlineSortDeparture []Interline | |
type interlineSortArrival []Interline | |
type interlineSortLegs []Interline | |
func sortInterlines(interlines []Interline, sortField, sortDirection string) ([]Interline, error) { | |
result, err := sortStats(interlinesSortable(interlines), sortField, sortDirection) | |
if err != nil { | |
return nil, err | |
} | |
return result.(interlinesSortable), nil | |
} | |
func (is interlinesSortable) Copy() Sortable { | |
result := make(interlinesSortable, len(is), len(is)) | |
copy(result, is) | |
return result | |
} | |
func (is interlinesSortable) GetSorter(fieldName string) sort.Interface { | |
switch fieldName { | |
case "duration": | |
return interlineSortDuration(is) | |
case "departure": | |
return interlineSortDeparture(is) | |
case "arrival": | |
return interlineSortArrival(is) | |
case "legs": | |
return interlineSortLegs(is) | |
} | |
return nil | |
} | |
func (is interlinesSortable) Supported() []string { | |
return []string{"duration", "departure", "arrival", "legs"} | |
} | |
func (is interlineSortDuration) Len() int { | |
return len(is) | |
} | |
func (is interlineSortDuration) Less(i, j int) bool { | |
return is[i].Duration < is[j].Duration | |
} | |
func (is interlineSortDuration) Swap(i, j int) { | |
is[i], is[j] = is[j], is[i] | |
} | |
func (is interlineSortDeparture) Len() int { | |
return len(is) | |
} | |
func (is interlineSortDeparture) Less(i, j int) bool { | |
return is[i].Departure.Before(is[j].Departure) | |
} | |
func (is interlineSortDeparture) Swap(i, j int) { | |
is[i], is[j] = is[j], is[i] | |
} | |
func (is interlineSortArrival) Len() int { | |
return len(is) | |
} | |
func (is interlineSortArrival) Less(i, j int) bool { | |
return is[i].Arrival.Before(is[j].Arrival) | |
} | |
func (is interlineSortArrival) Swap(i, j int) { | |
is[i], is[j] = is[j], is[i] | |
} | |
func (is interlineSortLegs) Len() int { | |
return len(is) | |
} | |
func (is interlineSortLegs) Less(i, j int) bool { | |
return len(is[i].Directs) < len(is[j].Directs) | |
} | |
func (is interlineSortLegs) Swap(i, j int) { | |
is[i], is[j] = is[j], is[i] | |
} | |
func main() { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment