Skip to content

Commit

Permalink
Merge pull request #46 from essentialkaos/develop
Browse files Browse the repository at this point in the history
Version 1.8.1
  • Loading branch information
andyone committed May 13, 2016
2 parents 7b92b74 + a64046e commit 938efbb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## Changelog

#### v1.8.1

* `[sortutil]` Added method `VersionCompare` which can be used for custom version sorting

#### v1.8.0

* `[sortutil]` Added case insensitive strings sorting
Expand Down
36 changes: 21 additions & 15 deletions sortutil/sortutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,27 @@ type stringSlice []string
func (s versionSlice) Len() int { return len(s) }
func (s versionSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s versionSlice) Less(i, j int) bool {
is := strings.Split(s[i], ".")
js := strings.Split(s[j], ".")
return VersionCompare(s[i], s[j])
}

func (s stringSlice) Len() int { return len(s) }
func (s stringSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s stringSlice) Less(i, j int) bool {
return strings.ToLower(s[i]) < strings.ToLower(s[j])
}

// ////////////////////////////////////////////////////////////////////////////////// //

// Versions sort versions slice
func Versions(s []string) {
sort.Sort(versionSlice(s))
}

// VersionsCompare compare 2 versions and return true if v1 less v2. This function
// can ve used for version sorting with structs
func VersionCompare(v1, v2 string) bool {
is := strings.Split(v1, ".")
js := strings.Split(v2, ".")

il, jl := len(is), len(js)

Expand Down Expand Up @@ -65,19 +84,6 @@ func (s versionSlice) Less(i, j int) bool {
return true
}

func (s stringSlice) Len() int { return len(s) }
func (s stringSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s stringSlice) Less(i, j int) bool {
return strings.ToLower(s[i]) < strings.ToLower(s[j])
}

// ////////////////////////////////////////////////////////////////////////////////// //

// Versions sort versions slice
func Versions(s []string) {
sort.Sort(versionSlice(s))
}

// Strings sort strings slice and support case insensitive mode
func Strings(s []string, caseInsensitive bool) {
if caseInsensitive {
Expand Down

0 comments on commit 938efbb

Please sign in to comment.