sort: github.com/golangplus/sort

## package sortp

`import "github.com/golangplus/sort"`

Package sortp is a plus to the standard "sort" package.

### func Bubble¶Uses

`func Bubble(data sort.Interface)`

The bubble sort algorithm. It is especially useful for almost sorted list. Bubble sort is a stable sort algorithm.

### func BubbleF¶Uses

`func BubbleF(Len int, Less func(i, j int) bool, Swap func(i, j int))`

Similar to Bubble but using closures other than sort.Interface.

Code:

```data := []int{5, 3, 1, 8, 0}

BubbleF(len(data), func(i, j int) bool {
return data[i] < data[j]
}, func(i, j int) {
data[i], data[j] = data[j], data[i]
})

fmt.Println(data)```

Output:

```[0 1 3 5 8]
```

### func DiffSortedList¶Uses

`func DiffSortedList(LeftLen, RightLen int, Compare func(l, r int) int, OutputLeft, OutputRight func(index int))`

DiffSortedList compares the difference of two sorted list. LeftLen and RightLen are lengths of the left/right lists, respectively. Compare returns -1/1/0 if l-th element of left list is less/greater/equal to the r-th element of the right list, both are zero-based. OutputLeft is called with the index of the element existing in the left list but not in the right list. OutputLeft will be called with ascending indexes. OutputRight is similar. Please make no assumption of the calling order of OutputLeft/OutputRight.

Code:

```from := []string{"a", "b", "d", "f"}
to := []string{"b", "c", "d", "g", "h"}

var extra, missing []string
DiffSortedList(len(from), len(to), func(f, t int) int {
if from[f] < to[t] {
return -1
}
if from[f] > to[t] {
return 1
}
return 0
}, func(f int) {
extra = append(extra, from[f])
}, func(t int) {
missing = append(missing, to[t])
})
fmt.Println("extra:", extra)
fmt.Println("missing:", missing)

from, to = to, from
extra, missing = nil, nil
DiffSortedList(len(from), len(to), func(f, t int) int {
if from[f] < to[t] {
return -1
}
if from[f] > to[t] {
return 1
}
return 0
}, func(f int) {
extra = append(extra, from[f])
}, func(t int) {
missing = append(missing, to[t])
})
fmt.Println("extra:", extra)
fmt.Println("missing:", missing)

to = from
extra, missing = nil, nil
DiffSortedList(len(from), len(to), func(f, t int) int {
if from[f] < to[t] {
return -1
}
if from[f] > to[t] {
return 1
}
return 0
}, func(f int) {
extra = append(extra, from[f])
}, func(t int) {
missing = append(missing, to[t])
})
fmt.Println("extra:", extra)
fmt.Println("missing:", missing)```

Output:

```extra: [a f]
missing: [c g h]
extra: [c g h]
missing: [a f]
extra: []
missing: []
```

### func IndexSort¶Uses

`func IndexSort(data sort.Interface) []int`

IndexSort returns a slice of indexes l, so that data[l[i]], i = 0...N-1, are sorted.

### func IndexSortF¶Uses

`func IndexSortF(Len int, Less func(i, j int) bool) []int`

IndexSortF is similar to IndexSort but with funcs as the input.

### func IndexStable¶Uses

`func IndexStable(data sort.Interface) []int`

IndexSort returns a slice of indexes l, so that data[l[i]], i = 0...N-1, are sorted.

### func IndexStableF¶Uses

`func IndexStableF(Len int, Less func(i, j int) bool) []int`

IndexStableF is similar to IndexStable but with funcs as the input.

### func IsSortedF¶Uses

`func IsSortedF(Len int, Less func(i, j int) bool) bool`

IsSortedF is similar to sort.IsSorted but with closure as arguments.

### func Merge¶Uses

`func Merge(LeftLen, RightLen int, Less func(l, r int) bool, AppendLeft func(l int), AppendRight func(r int))`

Merge merges two sorted list.

@param LeftLen is the length of the left list. @param RightLen is the length of the right list. @param Less compares the l-th element of left list and the r-th element of the right list. @param AppendLeft appends the l-th element of the left list to the result list. @param AppendRight appends the r-th element of the right list to the result list.

Code:

```left := []int{1, 3, 5, 7}
right := []int{4, 6, 8, 10, 11}

var merged []int
Merge(len(left), len(right), func(l, r int) bool {
return left[l] < right[r]
}, func(l int) {
merged = append(merged, left[l])
}, func(r int) {
merged = append(merged, right[r])
})
fmt.Println(merged)

left, right = right, left
merged = nil
Merge(len(left), len(right), func(l, r int) bool {
return left[l] < right[r]
}, func(l int) {
merged = append(merged, left[l])
}, func(r int) {
merged = append(merged, right[r])
})
fmt.Println(merged)```

Output:

```[1 3 4 5 6 7 8 10 11]
[1 3 4 5 6 7 8 10 11]
```

### func ReverseLess¶Uses

`func ReverseLess(Less func(i, j int) bool) func(i, j int) bool`

ReverseLess returns a func which can be used to sort data in a reverse order.

### func SortF¶Uses

`func SortF(Len int, Less func(i, j int) bool, Swap func(i, j int))`

SortF calls sort.Sort by closures. Since Interface.Len always returns a constant, it is an int parameter rather than a closure here.

### func StableF¶Uses

`func StableF(Len int, Less func(i, j int) bool, Swap func(i, j int))`

StableF calls sort.Stable by closures. Since Interface.Len always returns a constant, it is an int parameter rather than a closure here.

### type InterfaceStruct¶Uses

```type InterfaceStruct struct {
// Len is the number of elements in the collection.
LenF func() int
// Less reports whether the element with
// index i should sort before the element with index j.
LessF func(i, j int) bool
// Swap swaps the elements with indexes i and j.
SwapF func(i, j int)
}```

InterfaceStruct is a struct implementing sort.Interface given closures

Code:

```data := []int{5, 3, 1, 8, 0}

sort.Sort(InterfaceStruct{
LenF: func() int {
return len(data)
}, LessF: func(i, j int) bool {
return data[i] < data[j]
}, SwapF: func(i, j int) {
data[i], data[j] = data[j], data[i]
},
})

fmt.Println(data)```

Output:

```[0 1 3 5 8]
```

#### func (InterfaceStruct) Len¶Uses

`func (is InterfaceStruct) Len() int`

sort.Interface.Len

#### func (InterfaceStruct) Less¶Uses

`func (is InterfaceStruct) Less(i, j int) bool`

sort.Interface.Less

#### func (InterfaceStruct) Swap¶Uses

`func (is InterfaceStruct) Swap(i, j int)`

sort.Interface.Swap

### Directories ¶

PathSynopsis
lessPackage less can generates some useful less functions.

Package sortp imports 1 packages (graph) and is imported by 8 packages. Updated 2016-09-01. Refresh now. Tools for package owners.