Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "sort"
- "io/ioutil"
- "encoding/json"
- )
- type Friend struct {
- Fname string `json:"fname"`
- Sname string `json:"sname"`
- Gender string `json:"gender"`
- Height int `json:"height"`
- }
- func main() {
- content, err := ioutil.ReadFile("friends.json")
- if err != nil {
- fmt.Println(err.Error())
- }
- var friends []Friend
- err2 := json.Unmarshal(content, &friends)
- if err2 != nil {
- fmt.Println("Error JSON Unmarshalling")
- fmt.Println(err2.Error())
- }
- firstnameSort(friends)
- heightOrderSort(friends)
- }
- func firstnameSort(friends []Friend){
- sort.Slice(friends, func(i, j int) bool { return friends[i].Fname < friends[j].Fname })
- for _,x := range friends{
- fmt.Printf("%s %s %d \n",x.Fname , x.Sname, x.Height)
- }
- }
- func heightOrderSort(friends []Friend) {
- sort.Slice(friends, func(i, j int) bool {return friends[i].Height < friends[j].Height })
- fmt.Println("")
- for _,x := range friends{
- fmt.Printf("%s %s %d\n",x.Fname , x.Sname, x.Height)
- }
- }
Add Comment
Please, Sign In to add comment