Standard library / sort
sort.Stable()
Sorts a collection in place while preserving original order among equivalent elements. Expected stdout: '[a bb cc]\n'.
Formal Syntax & Structure
go syntaxgo
func Stable(data Interface)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| data | Interface | Collection implementing sort.Interface. |
Working Example
Example Snippetgo
package main
import ( "fmt" "sort")
type byLength []string
func (a byLength) Len() int { return len(a) }func (a byLength) Less(i, j int) bool { return len(a[i]) < len(a[j]) }func (a byLength) Swap(i, j int) { a[i], a[j] = a[j], a[i] }func main() { a := byLength{"bb", "a", "cc"} sort.Stable(a) fmt.Println(a)}