Order returns a derivative query with a field-based sort order. Orders are applied in the order they are added. The default order is ascending; to sort in descending order prefix the fieldName with a minus sign (-).
(fieldName string)
| 169 | // applied in the order they are added. The default order is ascending; to sort |
| 170 | // in descending order prefix the fieldName with a minus sign (-). |
| 171 | func (q *Query) Order(fieldName string) *Query { |
| 172 | q = q.clone() |
| 173 | fieldName = strings.TrimSpace(fieldName) |
| 174 | o := order{ |
| 175 | Direction: ascending, |
| 176 | FieldName: fieldName, |
| 177 | } |
| 178 | if strings.HasPrefix(fieldName, "-") { |
| 179 | o.Direction = descending |
| 180 | o.FieldName = strings.TrimSpace(fieldName[1:]) |
| 181 | } else if strings.HasPrefix(fieldName, "+") { |
| 182 | q.err = fmt.Errorf("datastore: invalid order: %q", fieldName) |
| 183 | return q |
| 184 | } |
| 185 | if len(o.FieldName) == 0 { |
| 186 | q.err = errors.New("datastore: empty order") |
| 187 | return q |
| 188 | } |
| 189 | q.order = append(q.order, o) |
| 190 | return q |
| 191 | } |
| 192 | |
| 193 | // Project returns a derivative query that yields only the given fields. It |
| 194 | // cannot be used with KeysOnly. |