Created
July 21, 2021 21:20
-
-
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"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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