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

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

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

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

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

英文:

For example,

package main

import (
	&quot;fmt&quot;
	&quot;sort&quot;
	&quot;time&quot;
)

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 &lt; s.Courses[j].Name }

func main() {
	var courses = Courses{
		&amp;Course{Name: &quot;John&quot;},
		&amp;Course{Name: &quot;Peter&quot;},
		&amp;Course{Name: &quot;Jane&quot;},
	}
	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(&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:

确定