# 10 examples of 'golang sort slice' in Go

Every line of 'golang sort slice' 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
``32func (slice UIntPtrSlice) SortAsc() UIntPtrSlice {33	sort.SliceStable(slice, func(i, j int) bool {34		return slice[i] < slice[j]35	})36	return slice37}``
``7func Sort(arr []int) []int{8	if len(arr) <= 1 {9		return arr10	}1112	n := len(arr) - 11314	for {15		if n == 0 {16			break17		}1819		for i := 0; i < len(arr)-1; i++ {20			if arr[i] > arr[i+1] {21				arr[i], arr[i+1] = arr[i+1], arr[i]22			}23		}24		n -= 125	}2627	return arr28}``
``17func Sort(arr []int) []int {18	for i := 0; i < len(arr); i++ {19		min := i20		for j := i + 1; j < len(arr); j++ {21			if arr[j] < arr[min] {22				min = j23			}24		}25		if min != i {26			arr[i], arr[min] = arr[min], arr[i]27		}28	}29	return arr30}``
``6func GnomeSort(arr []int) []int {7    for pos := 1; pos < len(arr); pos++ {8        if arr[pos] >= arr[pos-1] {9            continue10        } else {11            arr[pos], arr[pos-1] = arr[pos-1], arr[pos]12            if pos > 1 {13                pos -= 114            }15        }16        pos -= 117    }1819    return arr20}``
``30func quickSortUtil(arr []int, lower int, upper int) {31	if upper <= lower {32		return33	}34	pivot := arr[lower]35	start := lower36	stop := upper3738	for lower < upper {39		for arr[lower] <= pivot && lower < upper {40			lower++41		}42		for arr[upper] > pivot && lower <= upper {43			upper--44		}45		if lower < upper {46			swap(arr, upper, lower)47		}48	}49	swap(arr, upper, start) // upper is the pivot position50	quickSortUtil(arr, start, upper-1) // pivot -1 is the upper for left sub array.51	quickSortUtil(arr, upper+1, stop)  // pivot + 1 is the lower for right sub array.52}``
``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}``
``14func quickSort(A []int) {15	recursionSort(A, 0, len(A)-1)16}``
``11func quicksort(seq []int) []int {12	l := len(seq)13	if l <= 1 {14		return seq15	}16	s := seq[0]17	left := []int{}18	right := []int{}1920	for i := 1; i < l; i++ {21		if seq[i] <= s {22			left = append(left, seq[i])23		} else {24			right = append(right, seq[i])25		}26	}27	left = quicksort(left)28	right = quicksort(right)2930	seq = append(left, s)31	seq = append(seq, right...)3233	return seq34}``
``11func (ids idList) sort(reverse bool) {12	compareFunc := func(i, j int) bool {13		return ids[i].Compare(ids[j])14	}1516	if reverse {17		compareFunc = func(i, j int) bool {18			return ids[j].Compare(ids[i])19		}20	}2122	sort.Slice(ids, compareFunc)23}``
``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 > 0 {161			prev = ms[i-1].Version162			ms[i-1].Next = m.Version163		}164		ms[i].Previous = prev165	}166}``