type TDepartment struct {
Ctx context.Context json:"-" db:"-"
DeptId int64 json:"fdeptid" db:"fdeptid"
CorpId int64 json:"fcorpid" db:"fcorpid"
ParentDeptId int64 json:"fparentdeptid" db:"fparentdeptid"
Sort int32 json:"fsort" db:"fsort"
DeptName string json:"fdeptname" db:"fdeptname"
CreateTime string json:"fcreatetime" db:"fcreatetime"
ModifyTime string json:"fmodifytime" db:"fmodifytime"
}
type TDepartmentSlice []*TDepartment
func (p TDepartmentSlice) Len() int { return len(p) }
// 根据DeptId 排序
func (p TDepartmentSlice) Less(i, j int) bool {
return p[i].DeptId < p[j].DeptId
}
func (p TDepartmentSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
type SortTDepartmentSliceByCreateTime struct {
TDepartmentSlice
}
func (this SortTDepartmentSliceByCreateTime) Less(i, j int) bool {
t1, := time.Parse("2006-01-02 15:04:05", this.TDepartmentSlice[i].CreateTime)
t2, := time.Parse("2006-01-02 15:04:05", this.TDepartmentSlice[j].CreateTime)
return t1.Unix() < t2.Unix()
}
//func (this TDepartmentSlice) Swap(i, j int) { this[i], this[j] = this[j], this[i] }
func main() {
t1 := TDepartment{
DeptId: 1,
CreateTime: "2019-08-10 15:11:12",
}
t2 := TDepartment{
DeptId: 2,
CreateTime: "2019-08-10 15:11:23",
}
t3 := TDepartment{
DeptId: 4,
CreateTime: "2019-08-18 15:11:12",
}
t4 := TDepartment{
DeptId: 3,
CreateTime: "2019-08-08 15:11:12",
}
penson := TDepartmentSlice{}
penson = append(penson, &t1, &t2, &t3, &t4)
for , v := range penson {
fmt.Println(fmt.Sprintf("deptId:%v.createTime:%v", v.DeptId, v.CreateTime))
}
sort.Sort(TDepartmentSlice(penson))
for , v := range penson {
fmt.Println(fmt.Sprintf("deptId:%v.createTime:%v", v.DeptId, v.CreateTime))
}
sort.Sort(SortTDepartmentSliceByCreateTime{penson})
for _, v := range penson {
fmt.Println(fmt.Sprintf("deptId:%v.createTime:%v",v.DeptId,v.CreateTime))
}
}
评论