Skip to content

Array Sort

Serkan Algur edited this page Dec 5, 2020 · 3 revisions

Sort - Sort an array

func Sort(v interface{}, flag string) bool

Original : https://www.php.net/manual/en/function.sort.php

This function sorts an array. Elements will be arranged from lowest to highest when this function has completed. Flags are same as php sort.

SORT_REGULAR // compare items normally;
SORT_NUMERIC // compare items numerically
SORT_STRING // compare items as strings
SORT_NATURAL // compare items as strings using "natural ordering" like natsort()

Usage

package main

import (
	"fmt"
	"github.com/serkanalgur/phpfuncs"
)

func main() {
  za := []string{"Sistin","Neata","Zhopus","Norough","Zhestin","Xine","Glia","Aplerton","Agosding","Amdon"}
	va := []int{1,9,8,5,7,12,24,36,40,20,4,19}
  phpfuncs.Sort(za,"SORT_STRING")
	phpfuncs.Sort(va,"SORT_NUMERIC")
	fmt.Println(va,za)
}

// [1 4 5 7 8 9 12 19 20 24 36 40] [Agosding Amdon Aplerton Glia Neata Norough Sistin Xine Zhestin Zhopus]

▶️ Go Playground

Clone this wiki locally