1. 程式人生 > >go語言----對結構體進行排序

go語言----對結構體進行排序

package main

import  "fmt"

type myStruct struct {
	Sid 		int		//使用者sid
	NN			string
	Score 		int64		//分數
}

func SortUserScore(fc []myStruct) []myStruct{
	for i:=0;i<len(fc)-1;i++{
		for j:= i+1 ;j<len(fc);j++{
			if fc[i].Score < fc[j].Score{
				fc[i],fc[j] = fc[j],fc[i]
			}
		}
	}
	return fc
}

func main() {
	var myList = make([]myStruct,0)
	myList = append(myList,myStruct{3,"3",3})
	myList = append(myList,myStruct{1,"1",1})
	myList = append(myList,myStruct{2,"2",2})
	fmt.Println(myList)
	sl := SortUserScore(myList)
	fmt.Println(sl)
	
}

執行結果如下: