reverse.go 712 B

123456789101112131415161718192021222324252627282930
  1. package help
  2. // Reverse inverts the order of the elements in a collection.
  3. //
  4. // Unlike OrderBy, this sorting method does not consider the actual values
  5. // themselves in determining the order. Rather, it just returns the elements in
  6. // the reverse order from which they are produced by the underlying source.
  7. func (q Query) Reverse() Query {
  8. return Query{
  9. Iterate: func() Iterator {
  10. next := q.Iterate()
  11. items := []interface{}{}
  12. for item, ok := next(); ok; item, ok = next() {
  13. items = append(items, item)
  14. }
  15. index := len(items) - 1
  16. return func() (item interface{}, ok bool) {
  17. if index < 0 {
  18. return
  19. }
  20. item, ok = items[index], true
  21. index--
  22. return
  23. }
  24. },
  25. }
  26. }