Godeps/_workspace/src/gopkg.in/mgo.v2/session.go
func (q *Query) Sort(fields ...string) *Query {
	q.m.Lock()
	var order bson.D
	for _, field := range fields {
		n := 1
		var kind string
		if field != "" {
			if field[0] == '$' {
				if c := strings.Index(field, ":"); c > 1 && c < len(field)-1 {
					kind = field[1:c]
					field = field[c+1:]
				}
			}
			switch field[0] {
			case '+':
				field = field[1:]
			case '-':
				n = -1
				field = field[1:]
			}
		}
		if field == "" {
			panic("Sort: empty field name")
		}
		if kind == "textScore" {
			order = append(order, bson.DocElem{field, bson.M{"$meta": kind}})
		} else {
			order = append(order, bson.DocElem{field, n})
		}
	}
	q.op.options.OrderBy = order
	q.op.hasOptions = true
	q.m.Unlock()
	return q
}
func (q *Query) Sort(fields ...string) *Query {
	q.m.Lock()
	var order bson.D
	for _, field := range fields {
		n := 1
		var kind string
		if field != "" {
			if field[0] == '$' {
				if c := strings.Index(field, ":"); c > 1 && c < len(field)-1 {
					kind = field[1:c]
					field = field[c+1:]
				}
			}
			switch field[0] {
			case '+':
				field = field[1:]
			case '-':
				n = -1
				field = field[1:]
			}
		}
		if field == "" {
			panic("Sort: empty field name")
		}
		if kind == "textScore" {
			order = append(order, bson.DocElem{field, bson.M{"$meta": kind}})
		} else {
			order = append(order, bson.DocElem{field, n})
		}
	}
	q.op.options.OrderBy = order
	q.op.hasOptions = true
	q.m.Unlock()
	return q
}
func ( q * Query ) Sort ( fields ... string ) * Query { q . m . Lock ( ) var order bson . D for _ , field := range fields { n := 1 var kind string if field != " " { if field [ 0 ] == '$' { if c := strings . Index ( field , " " ) ; c > 1 && c < len ( field ) - 1 { kind = field [ 1 : c ] field = field [ c + 1 : ] } } switch field [ 0 ] { case '+' : field = field [ 1 : ] case '-' : n = - 1 field = field [ 1 : ] } } if field == " " { panic ( " " ) } if kind == " " { order = append ( order , bson . DocElem { field , bson . M { " " : kind } } ) } else { order = append ( order , bson . DocElem { field , n } ) } } q . op . options . OrderBy = order q . op . hasOptions = true q . m . Unlock ( ) return q }
// Sort asks the database to order returned documents according to the
// provided field names. A field name may be prefixed by - (minus) for
// it to be sorted in reverse order.
//
// For example:
//
//     query1 := collection.Find(nil).Sort("firstname", "lastname")
//     query2 := collection.Find(nil).Sort("-age")
//     query3 := collection.Find(nil).Sort("$natural")
//     query4 := collection.Find(nil).Select(bson.M{"score": bson.M{"$meta": "textScore"}}).Sort("$textScore:score")
//
// Relevant documentation:
//
//     http://www.mongodb.org/display/DOCS/Sorting+and+Natural+Order
//
Sort asks the database to order returned documents according to the provided field names . A field name may be prefixed by - ( minus ) for it to be sorted in reverse order . For example : query1 : = collection . Find ( nil ) . Sort ( firstname lastname ) query2 : = collection . Find ( nil ) . Sort ( - age ) query3 : = collection . Find ( nil ) . Sort ( $natural ) query4 : = collection . Find ( nil ) . Select ( bson . M { score : bson . M { $meta : textScore }} ) . Sort ( $textScore : score ) Relevant documentation : http : // www . mongodb . org / display / DOCS / Sorting + and + Natural + Order