I have seen this question asked in a few other languages, each with great answers. i.e.
How to sort a file by line length and then alphabetically for the second key?
how to sort by length of string followed by alphabetical order?
Sort strings alphabetically AND by length?
I can't figure it out in Golang :/ Say I have this list:
2 22 2H 2J 2J2 2J3 2J322422 2J322423 2J33 2M 2P 2W 2X
I want the sorted output to be:
2 22 2H 2J 2M 2P 2W 2X 2J2 2J3 2J33 2J322422 2J322423
I have tried a few things in Golang, but just can't get it working.
// Log
t.Log.Println(values)
// Sort values alphabetically
sort.Strings(values)
// Sort values by length
sort.Slice(values, func(i, j int) bool {
return len(values[i]) < len(values[j])
})
// Log
t.Log.Println(values)