# 10 examples of 'go sort' in Go

Every line of 'go sort' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Go code is secure.

## All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
``17func Sort(arr []int) []int {18	for i := 0; i &lt; len(arr); i++ {19		min := i20		for j := i + 1; j &lt; len(arr); j++ {21			if arr[j] &lt; arr[min] {22				min = j23			}24		}25		if min != i {26			arr[i], arr[min] = arr[min], arr[i]27		}28	}29	return arr30}``
``7func Sort(arr []int) []int{8	if len(arr) &lt;= 1 {9		return arr10	}1112	n := len(arr) - 11314	for {15		if n == 0 {16			break17		}1819		for i := 0; i &lt; len(arr)-1; i++ {20			if arr[i] &gt; arr[i+1] {21				arr[i], arr[i+1] = arr[i+1], arr[i]22			}23		}24		n -= 125	}2627	return arr28}``
``147func (ms migrationSorter) Sort(direction bool) {148149	// sort ascending or descending by version150	if direction {151		sort.Sort(ms)152	} else {153		sort.Sort(sort.Reverse(ms))154	}155156	// now that we're sorted in the appropriate direction,157	// populate next and previous for each migration158	for i, m := range ms {159		prev := int64(-1)160		if i &gt; 0 {161			prev = ms[i-1].Version162			ms[i-1].Next = m.Version163		}164		ms[i].Previous = prev165	}166}``
``21func (v *ListStore) Sort(compareFunc CompareDataFunc) {22	C._g_list_store_sort(v.native(), C.gpointer(callback.Assign(compareFunc)))23}``
``68func (n *MapLayerEndpoint) Sort(params []*m.SortMapLayer) (err error) {6970	for _, s := range params {71		n.adaptors.MapLayer.Sort(&amp;m.MapLayer{72			Id:     s.Id,73			Weight: s.Weight,74		})75	}7677	return78}``
``22func sort(a, b, c int) (int, int, int) {23	if c &lt; a {24		a, c = c, a25	}26	if b &lt; a {27		a, b = b, a28	}29	if c &lt; b {30		b, c = c, b31	}32	return a, b, c33}``
``6func GnomeSort(arr []int) []int {7    for pos := 1; pos &lt; len(arr); pos++ {8        if arr[pos] &gt;= arr[pos-1] {9            continue10        } else {11            arr[pos], arr[pos-1] = arr[pos-1], arr[pos]12            if pos &gt; 1 {13                pos -= 114            }15        }16        pos -= 117    }1819    return arr20}``
``274func SortImpl(args *Data, env *SymbolTableFrame) (result *Data, err error) {275	coll := Car(args)276	if !ListP(coll) {277		err = ProcessError("sort requires a list as it's first argument.", env)278		return279	}280281	proc := Cadr(args)282	if !FunctionOrPrimitiveP(proc) {283		err = ProcessError("sort requires a function or primitive as it's second argument.", env)284		return285	}286287	arr := ToArray(coll)288289	sort.Slice(arr, func(i, j int) bool {290		var ret bool291		if err == nil {292			a := arr[i]293			b := arr[j]294			ret, err = sortCompare(a, b, proc, env)295		}296		return ret297	})298299	return ArrayToList(arr), err300}``
``72func (blocks Blocks) Sort() {73	sort.Slice(blocks, func(i, j int) bool {74		return blocks[i].Behind(blocks[j])75	})76}``
``453func (g *DirectCandidateGenerator) Sort(sort string) *DirectCandidateGenerator {454	g.sort = &amp;sort455	return g456}``