Skip to content

Instantly share code, notes, and snippets.

@kai5263499
Created July 21, 2021 21:20
Show Gist options
  • Save kai5263499/95f48212a5f144c625acff012dcce4d9 to your computer and use it in GitHub Desktop.
Save kai5263499/95f48212a5f144c625acff012dcce4d9 to your computer and use it in GitHub Desktop.
Turn a string of ints like "1,3,2" into a sorted string like "1,2,3"
func sortStringOfInts(unsorted string) (string, error) {
unsortedStringElements := strings.Split(unsorted, ",")
intSlice := make([]int, len(unsortedStringElements))
for i, s := range unsortedStringElements {
intSlice[i], _ = strconv.Atoi(s)
}
sort.Ints(intSlice)
sortedStringElements := make([]string, len(unsortedStringElements))
for i, s := range intSlice {
sortedStringElements[i] = strconv.Itoa(s)
}
return strings.Join(sortedStringElements[:], ","), nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment