Another, OO-ish fix is to use method on custom type 
(https://play.golang.org/p/a5S5rNog5h):

type itinerary []string

func (t itinerary) sort() {
    sort.Slice(t, func(i, j int) bool {
        return t[i] < t[j]
    })
}

func main() {
    itinerary := itinerary{"Philadelphia", "Chicago", "Boston", "Austin"}
    itinerary[1:].sort()
    fmt.Println("My new itinerary is", itinerary)
}


ain


On Wednesday, March 8, 2017 at 5:01:08 PM UTC+2, Val wrote:
>
> Sorry for not elaborating in the first place (I was trying to make the 
> point very concise).
>
> This is the starting city :  Philadelphia
>
> This are the other cities :   Chicago, Boston, Austin
>
> The itinerary is the concatenation of starting city + other cities to be 
> visited :   [Philadelphia, Chicago, Boston, Austin]
>
> In this contrived example the first city is fixed, because this is where I 
> am now, wherever I decide to go next. That's why I decide to reorder all 
> cities of my itinerary except the first one.
>
> This is what my naive code what trying to achieve (expected value of slice 
> itinerary after partial sort) : [Philadelphia Austin Boston Chicago]
>
> This is what the code actually produces (value of slice itinerary after 
> partial sort) : [Philadelphia Boston Chicago Austin] , which is why i say 
> it is broken : the last 3 items don't end up in alphabetical order like I 
> expected them to be.
>
> In a real program, there would be various legit reasons to sort only part 
> of a slice, and the items would not always have a builtin type like string, 
> rather custom struct types.
>
> @CBanning  Thanks, I had not thought of StringSlice!  But it's not 
> applicable in my real programs, which deal with custom struct types (not 
> strings).
>
> @David  Sorting the whole slice is not what I was trying to achieve, 
> because I can't change my starting point.
>
> 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 
> <https://play.golang.org/p/v0V8BQuJ_m> or fix 2 
> <https://play.golang.org/p/z_52IQAhY4> .
>
> 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 
> <https://play.golang.org/p/ZgHyGOLiQO> .
>
> Best regards
> Val
>
> On Wednesday, March 8, 2017 at 3:25:48 PM UTC+1, David Peacock wrote:
>>
>> On Wed, Mar 8, 2017 at 8:32 AM, Valentin Deleplace <dele...@gmail.com> 
>> wrote:
>>
>>> I did explain the expected result : "and I want to visit other cities 
>>> in alphabetical order"
>>>
>>
>> Jan is correct; the characterization of a problem hinges on accurately 
>> describing what you expect and what happened instead.  Terms such as 
>> "broken" and "does not work" are best avoided because others don't know 
>> what your definitions are for these in the given context. :-)
>>
>> That being said, your alphabetical order is returned as you intend if you 
>> adjust line 17 thusly:
>>
>> sort.Slice(itinerary[:], func(i, j int) bool {
>>
>> Cheers,
>> David
>>  
>>
>>>
>>> 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...@googlegroups.com.
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>
>>

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to