本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

30 lines
771B

  1. // Copyright (c) 2015, Emir Pasic. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package utils
  5. import "sort"
  6. // Sort sorts values (in-place) with respect to the given comparator.
  7. //
  8. // Uses Go's sort (hybrid of quicksort for large and then insertion sort for smaller slices).
  9. func Sort(values []interface{}, comparator Comparator) {
  10. sort.Sort(sortable{values, comparator})
  11. }
  12. type sortable struct {
  13. values []interface{}
  14. comparator Comparator
  15. }
  16. func (s sortable) Len() int {
  17. return len(s.values)
  18. }
  19. func (s sortable) Swap(i, j int) {
  20. s.values[i], s.values[j] = s.values[j], s.values[i]
  21. }
  22. func (s sortable) Less(i, j int) bool {
  23. return s.comparator(s.values[i], s.values[j]) < 0
  24. }
上海开阖软件有限公司 沪ICP备12045867号-1