Guest User

Untitled

a guest
Nov 18th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "sort"
  6. "io/ioutil"
  7. "encoding/json"
  8. )
  9.  
  10. type Friend struct {
  11. Fname string `json:"fname"`
  12. Sname string `json:"sname"`
  13. Gender string `json:"gender"`
  14. Height int `json:"height"`
  15. }
  16.  
  17. func main() {
  18.  
  19. content, err := ioutil.ReadFile("friends.json")
  20. if err != nil {
  21. fmt.Println(err.Error())
  22. }
  23.  
  24. var friends []Friend
  25.  
  26. err2 := json.Unmarshal(content, &friends)
  27.  
  28. if err2 != nil {
  29. fmt.Println("Error JSON Unmarshalling")
  30. fmt.Println(err2.Error())
  31. }
  32. firstnameSort(friends)
  33. heightOrderSort(friends)
  34.  
  35. }
  36. func firstnameSort(friends []Friend){
  37. sort.Slice(friends, func(i, j int) bool { return friends[i].Fname < friends[j].Fname })
  38. for _,x := range friends{
  39. fmt.Printf("%s %s %d \n",x.Fname , x.Sname, x.Height)
  40. }
  41. }
  42. func heightOrderSort(friends []Friend) {
  43. sort.Slice(friends, func(i, j int) bool {return friends[i].Height < friends[j].Height })
  44. fmt.Println("")
  45. for _,x := range friends{
  46. fmt.Printf("%s %s %d\n",x.Fname , x.Sname, x.Height)
  47. }
  48. }
Add Comment
Please, Sign In to add comment