-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added: SortStableUsing and SortUsing (#103)
SortStableUsing works similar to sort.SliceStable. However, unlike sort.SliceStable the slice returned will be reallocated as to not modify the input slice. SortUsing works similar to sort.Slice. However, unlike sort.Slice the slice returned will be reallocated as to not modify the input slice. Fixes #98
- Loading branch information
1 parent
55d5be7
commit aa5d6ff
Showing
14 changed files
with
416 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package functions | ||
|
||
import ( | ||
"sort" | ||
) | ||
|
||
// SortStableUsing works similar to sort.SliceStable. However, unlike sort.SliceStable the | ||
// slice returned will be reallocated as to not modify the input slice. | ||
func (ss SliceType) SortStableUsing(less func(a, b ElementType) bool) SliceType { | ||
// Avoid the allocation. If there is one element or less it is already | ||
// sorted. | ||
if len(ss) < 2 { | ||
return ss | ||
} | ||
|
||
sorted := make(SliceType, len(ss)) | ||
copy(sorted, ss) | ||
sort.SliceStable(sorted, func(i, j int) bool { | ||
return less(sorted[i], sorted[j]) | ||
}) | ||
|
||
return sorted | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package functions | ||
|
||
import ( | ||
"sort" | ||
) | ||
|
||
// SortUsing works similar to sort.Slice. However, unlike sort.Slice the | ||
// slice returned will be reallocated as to not modify the input slice. | ||
func (ss SliceType) SortUsing(less func(a, b ElementType) bool) SliceType { | ||
// Avoid the allocation. If there is one element or less it is already | ||
// sorted. | ||
if len(ss) < 2 { | ||
return ss | ||
} | ||
|
||
sorted := make(SliceType, len(ss)) | ||
copy(sorted, ss) | ||
sort.Slice(sorted, func(i, j int) bool { | ||
return less(sorted[i], sorted[j]) | ||
}) | ||
|
||
return sorted | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.