sort.go 689 B

123456789101112131415161718192021222324252627282930313233343536
  1. package version
  2. import (
  3. "sort"
  4. )
  5. // Sorts a string slice of version number strings using version.CompareSimple()
  6. //
  7. // Example:
  8. // version.Sort([]string{"1.10-dev", "1.0rc1", "1.0", "1.0-dev"})
  9. // Returns []string{"1.0-dev", "1.0rc1", "1.0", "1.10-dev"}
  10. //
  11. func Sort(versionStrings []string) {
  12. versions := versionSlice(versionStrings)
  13. sort.Sort(versions)
  14. }
  15. type versionSlice []string
  16. func (s versionSlice) Len() int {
  17. return len(s)
  18. }
  19. func (s versionSlice) Less(i, j int) bool {
  20. cmp := CompareSimple(Normalize(s[i]), Normalize(s[j]))
  21. if cmp == 0 {
  22. return s[i] < s[j]
  23. }
  24. return cmp < 0
  25. }
  26. func (s versionSlice) Swap(i, j int) {
  27. tmp := s[j]
  28. s[j] = s[i]
  29. s[i] = tmp
  30. }