如何在GAE Go中对切片进行排序

huangapple go评论88阅读模式
英文:

How to sort slices in GAE Go

问题

我正在尝试对切片进行排序。如何在使用Go的GAE中实现这个功能?

我有一个结构体

  1. type courseData struct {
  2. Key *datastore.Key
  3. FormKey *datastore.Key
  4. Selected bool
  5. User string
  6. Name string
  7. Description string
  8. Date time.Time
  9. }

我想按照Name字段对这个实体类型的切片进行排序。

  1. q := datastore.NewQuery("Course")
  2. var courses []*courseData
  3. if keys, err := q.GetAll(c, &courses); err != nil {
  4. http.Error(w, err.Error(), http.StatusInternalServerError)
  5. return
  6. } else {
  7. for i := range courses {
  8. courses[i].Key = keys[i]
  9. }
  10. }

我尝试了

  1. Sort(data Interface)

但不确定如何使用它。请帮忙。谢谢!

英文:

I am trying to sort slices. How to this in gae using go?

I have struct

  1. type courseData struct {
  2. Key *datastore.Key
  3. FormKey *datastore.Key
  4. Selected bool
  5. User string
  6. Name string
  7. Description string
  8. Date time.Time
  9. }

I would like to sort slice of this entity kind in the Name field.

  1. q := datastore.NewQuery("Course")
  2. var courses []*courseData
  3. if keys, err := q.GetAll(c, &courses); err != nil {
  4. http.Error(w, err.Error(), http.StatusInternalServerError)
  5. return
  6. } else {
  7. for i := range courses {
  8. courses[i].Key = keys[i]
  9. }
  10. }

I tried the

  1. Sort(data Interface)

but not sure how to use it.
Please help. Thanks!

答案1

得分: 2

例如,

  1. package main
  2. import (
  3. "fmt"
  4. "sort"
  5. "time"
  6. )
  7. type Course struct {
  8. Key string // *datastore.Key
  9. FormKey string // *datastore.Key
  10. Selected bool
  11. User string
  12. Name string
  13. Description string
  14. Date time.Time
  15. }
  16. type Courses []*Course
  17. func (s Courses) Len() int { return len(s) }
  18. func (s Courses) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
  19. type ByName struct{ Courses }
  20. func (s ByName) Less(i, j int) bool { return s.Courses[i].Name < s.Courses[j].Name }
  21. func main() {
  22. var courses = Courses{
  23. &Course{Name: "John"},
  24. &Course{Name: "Peter"},
  25. &Course{Name: "Jane"},
  26. }
  27. sort.Sort(ByName{courses})
  28. for _, course := range courses {
  29. fmt.Println(course.Name)
  30. }

输出:

  1. Jane
  2. John
  3. Peter

CourseCourses需要被导出以供sort包使用。

为了避免示例依赖于GAE,类型*datastore.Key被更改为string

英文:

For example,

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;sort&quot;
  5. &quot;time&quot;
  6. )
  7. type Course struct {
  8. Key string // *datastore.Key
  9. FormKey string // *datastore.Key
  10. Selected bool
  11. User string
  12. Name string
  13. Description string
  14. Date time.Time
  15. }
  16. type Courses []*Course
  17. func (s Courses) Len() int { return len(s) }
  18. func (s Courses) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
  19. type ByName struct{ Courses }
  20. func (s ByName) Less(i, j int) bool { return s.Courses[i].Name &lt; s.Courses[j].Name }
  21. func main() {
  22. var courses = Courses{
  23. &amp;Course{Name: &quot;John&quot;},
  24. &amp;Course{Name: &quot;Peter&quot;},
  25. &amp;Course{Name: &quot;Jane&quot;},
  26. }
  27. sort.Sort(ByName{courses})
  28. for _, course := range courses {
  29. fmt.Println(course.Name)
  30. }

Output:

  1. Jane
  2. John
  3. 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

为什么不直接从数据存储中按正确的顺序请求实体?

  1. q := datastore.NewQuery("Course").Order("Name")
英文:

Why not just ask for the entities in the correct order from the datastore?

  1. q := datastore.NewQuery(&quot;Course&quot;).Order(&quot;Name&quot;)

huangapple
  • 本文由 发表于 2013年3月29日 17:52:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/15700908.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定