Skip to content

Instantly share code, notes, and snippets.

Created April 16, 2012 06:29
Show Gist options
  • Save anonymous/2396705 to your computer and use it in GitHub Desktop.
Save anonymous/2396705 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"sort"
)
type Triangle struct {
h int
w int
}
type Triangles []Triangle
func (t *Triangle) Area() (a int) {
a = (t.h * t.w) / 2
return
}
func (t Triangles) Less(i, j int) bool {
if t[i].Area() < t[j].Area() {
return true
}
return false
}
func (g Triangles) Swap(i, j int) {
g[i], g[j] = g[j], g[i]
}
func (t Triangles) Len() int {
return len(t)
}
func main() {
tg := Triangles{
Triangle{2, 3},
Triangle{2, 8},
Triangle{3, 9},
Triangle{3, 8},
Triangle{3, 18},
Triangle{9, 1},
Triangle{3, 8},
}
sort.Sort(tg)
for _, t := range tg {
fmt.Println(t)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment