//
// I am in Philadelphia, and I want to visit other cities
//
itinerary := []string{"Philadelphia", "Chicago", "Boston", "Austin"}
//
// I am in Philadelphia, and I want to visit other cities in alphabetical order
//
sort.Slice(itinerary[1:], func(i, j int) bool {
return itinerary[i] < itinerary[j]
})-j
I did explain the expected result : "and I want to visit other cities in alphabetical order"
Le 8 mars 2017 2:16 PM, "Jan Mercl" <0xj...@gmail.com> a écrit :On Wed, Mar 8, 2017 at 2:10 PM Val <dele...@gmail.com> wrote:
> What do you think?You should explain what you've expected to get instead of what you've got. Without that, I for one, cannot figure out what you see as broken and I have no idea why do you think it's not a good idea to sort a slice of a slice. After all, it's just a slice as any other.---j
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
Sorry for not elaborating in the first place (I was trying to make the point very concise).
The bug in my code is that the two arguments I pass to sort.Slice are inconsistent : the portion to be sorted is a reslicing from itinerary, while the "less" closure indexes items of the whole itinerary.
I brought up the "go vet" idea because I feel that whenever someone will do some reslicing directly in the first argument,
- either the result will be "broken" like mine,
- or the code in the less function will have to refer to the same (unnamed) resliced portion, which is imo convoluted : fix 1 or fix 2 .
The most readable alternative I can think of while still using sort.Slice is to reslice in a new variable prior to sorting : fix 3 .