Sort numbers by the remainder

Report a typo

Suppose you have a slice nums of int numbers. Your task is to sort the nums slice by the remainder of dividing each number by 5 and then output the resulting slice.

To do this, you'll need to use sort.SliceStable() function with an anonymous function that performs the modulo operation %5 when comparing the two numbers.

Note that you could also use the sort.Slice() function to solve this task because the slice is small and only has four numbers. However, if the slice had thousands of numbers, it would be better to use the sort.SliceStable() function to guarantee that the sorted numbers preserve the same initial order.

Sample Input 1:

6 1 0 0

Sample Output 1:

[0 0 6 1]
Write a program in Go
package main

import (
"fmt"
"sort"
)

func main() {
// DO NOT modify the code block below, it reads 4 int numbers from the stdin:
var num1, num2, num3, num4 int
fmt.Scanln(&num1, &num2, &num3, &num4)
nums := []int{num1, num2, num3, num4}

// Use the sort.SliceStable() function to sort the `nums` slice
// by the remainder of dividing each number by 5 below:
?

fmt.Println(nums) // output the sorted slice here!
}
___

Create a free account to access the full topic