Skip to content

Commit

Permalink
Documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
fredericlemoine committed Nov 3, 2017
1 parent 1443a6a commit e89700b
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions sort/sort.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
/*
This package provides a few functions to sort int arrays.
* Sort by values of another array
* Return permutation allowing to sort input array (like R order() function)
Inspired from https://stackoverflow.com/questions/42707252/
*/
package sort

import (
"sort"
)

// See https://stackoverflow.com/questions/42707252/

type by struct {
Indices []int
Values []int
Expand All @@ -18,10 +22,11 @@ func (b by) Swap(i, j int) {
b.Values[i], b.Values[j] = b.Values[j], b.Values[i]
}

/*
Sorts "toSort" array according to "byValues" Array.
Only "toSort" array is modified. "byValues" array is not modified.
*/
// Sorts "toSort" array according to "byValues" Array.
// * "toSort" array is modified after the function.
// * "byValues" array is not modified.
//
// The sort may be in decreasing order.
func SortIntBy(toSort []int, byValues []int, decreasing bool) {
valuescopy := make([]int, len(byValues))
copy(valuescopy, byValues)
Expand All @@ -32,16 +37,16 @@ func SortIntBy(toSort []int, byValues []int, decreasing bool) {
}
}

/*
Returns sorted indices by values
Example:
Input: [10,20,30,80,50]
Outpt: [0,1,2,4,3]
To range over the sorted array :
for _,ord := range output {
input[ord]
}
*/
// Returns reordered indices (permutation) by values (like order() R function).
// It may be in decreasing order.
//
// Example:
// input := []int{10,20,30,80,50}
// output := OrderInt(input) // {0,1,2,4,3}
// // To iterate over the sorted array :
// for _,ord := range output {
// input[ord]
// }
func OrderInt(values []int, decreasing bool) []int {
// init initial order indices
indices := make([]int, len(values))
Expand Down

0 comments on commit e89700b

Please sign in to comment.