英文:
How to sort slices in GAE Go
问题
我正在尝试对切片进行排序。如何在使用Go的GAE中实现这个功能?
我有一个结构体
type courseData struct {
Key *datastore.Key
FormKey *datastore.Key
Selected bool
User string
Name string
Description string
Date time.Time
}
我想按照Name字段对这个实体类型的切片进行排序。
q := datastore.NewQuery("Course")
var courses []*courseData
if keys, err := q.GetAll(c, &courses); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
} else {
for i := range courses {
courses[i].Key = keys[i]
}
}
我尝试了
Sort(data Interface)
但不确定如何使用它。请帮忙。谢谢!
英文:
I am trying to sort slices. How to this in gae using go?
I have struct
type courseData struct {
Key *datastore.Key
FormKey *datastore.Key
Selected bool
User string
Name string
Description string
Date time.Time
}
I would like to sort slice of this entity kind in the Name field.
q := datastore.NewQuery("Course")
var courses []*courseData
if keys, err := q.GetAll(c, &courses); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
} else {
for i := range courses {
courses[i].Key = keys[i]
}
}
I tried the
Sort(data Interface)
but not sure how to use it.
Please help. Thanks!
答案1
得分: 2
例如,
package main
import (
"fmt"
"sort"
"time"
)
type Course struct {
Key string // *datastore.Key
FormKey string // *datastore.Key
Selected bool
User string
Name string
Description string
Date time.Time
}
type Courses []*Course
func (s Courses) Len() int { return len(s) }
func (s Courses) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
type ByName struct{ Courses }
func (s ByName) Less(i, j int) bool { return s.Courses[i].Name < s.Courses[j].Name }
func main() {
var courses = Courses{
&Course{Name: "John"},
&Course{Name: "Peter"},
&Course{Name: "Jane"},
}
sort.Sort(ByName{courses})
for _, course := range courses {
fmt.Println(course.Name)
}
输出:
Jane
John
Peter
Course
和Courses
需要被导出以供sort
包使用。
为了避免示例依赖于GAE,类型*datastore.Key
被更改为string
。
英文:
For example,
package main
import (
"fmt"
"sort"
"time"
)
type Course struct {
Key string // *datastore.Key
FormKey string // *datastore.Key
Selected bool
User string
Name string
Description string
Date time.Time
}
type Courses []*Course
func (s Courses) Len() int { return len(s) }
func (s Courses) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
type ByName struct{ Courses }
func (s ByName) Less(i, j int) bool { return s.Courses[i].Name < s.Courses[j].Name }
func main() {
var courses = Courses{
&Course{Name: "John"},
&Course{Name: "Peter"},
&Course{Name: "Jane"},
}
sort.Sort(ByName{courses})
for _, course := range courses {
fmt.Println(course.Name)
}
Output:
Jane
John
Peter
Course
and Courses
need to be exported for use by the sort
package.
To avoid making the example dependent on GAE, type *datastore.Key
was changed to string
.
答案2
得分: 2
为什么不直接从数据存储中按正确的顺序请求实体?
q := datastore.NewQuery("Course").Order("Name")
英文:
Why not just ask for the entities in the correct order from the datastore?
q := datastore.NewQuery("Course").Order("Name")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论