Getting the first digit of an int

Viewed 3513

How do I sort a slice of ints by the first digit of each int?

I am trying to write my own custom sort:

type ByFirstDigit []int

func (s ByFirstDigit) Len() int {
    return len(s)
}

func (s ByFirstDigit) Swap(i, j int) {
    s[i], s[j] = s[j], s[i]
}

func (s ByFirstDigit) Less(i, j int) bool {
    return s[i][0] < s[j][0]
}

But I get this error:

s[j][0] (type int does not support indexing)

3 Answers
Related