Created
November 18, 2016 04:44
-
-
Save yuya-matsushima/f95a272509d44c4d6dca95f9be887c6f to your computer and use it in GitHub Desktop.
構造体スライスを使って sort 実装
This file contains hidden or 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" | |
"log" | |
"sort" | |
"strings" | |
) | |
type filter struct { | |
Priority int64 | |
Selector string | |
} | |
type filters []filter | |
func (f filters) String() string { | |
var s []string | |
for i := 0; i < len(f); i++ { | |
s = append(s, fmt.Sprintf("%d:%s", f[i].Priority, f[i].Selector)) | |
} | |
return strings.Join(s, ",") | |
} | |
func (f filters) Len() int { | |
return len(f) | |
} | |
func (f filters) Swap(i, j int) { | |
f[i], f[j] = f[j], f[i] | |
} | |
func (f filters) Less(i, j int) bool { | |
return f[i].Priority < f[j].Priority | |
} | |
func main() { | |
fs := filters{ | |
{4, "s4"}, | |
{2, "s2"}, | |
{3, "s3"}, | |
{1, "s1"}, | |
{6, "s6"}, | |
{5, "s5"}, | |
} | |
log.Print(fs.String()) | |
sort.Sort(fs) | |
log.Print(fs.String()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment