Oh, thanks a lot Egon, 

Your neat and elegant code is very helpful for me to get the idea and get 
started. 

Just FTA, in case someone is also reading this, more onto *sorting the 
slice*, here is my archive from this list:

https://groups.google.com/d/msg/golang-nuts/WV4d-0UTvLo/tJOoqOf9AgAJ

I want to write three different go functions that sorts based on age, name 
and sal.

The traditional way to achieve this in go (as showcased here 
<https://golang.org/pkg/sort/#example_> and here 
<http://www.programming-idioms.org/idiom/28/sort-by-a-property/160/go>) is, 
to :

- declare a named type of slice

- implement 3 methods Len, Less, Swap 
<https://golang.org/pkg/sort/#Interface>

- call sort.Sort <https://golang.org/pkg/sort/#Sort>

(each attribute age, name, sal, needs its own type declaration and 3 method 
implementations).

Since Go 1.8, we can achieve basically the same with much less boilerplate 
code (as showcased here <https://golang.org/pkg/sort/#example_Slice> and 
here <http://www.programming-idioms.org/idiom/28/sort-by-a-property/1936/go>) 
:

- implement 1 func "less" having type  func(i, j int) bool

- call sort.Slice <https://golang.org/pkg/sort/#Slice>

(each attribute age, name, sal, needs its own "less" func).

I suggest the latter.

HTH

Valentin




On Wednesday, August 16, 2017 at 7:20:40 AM UTC-4, Egon wrote:
>
> Do you mean ordered map or sorted map... either way, as long as the N is 
> small, then you can use a slice to provide the extra functionality:
>
> 1. sorted map https://play.golang.org/p/oLM4u5HwQ6
> 2. ordered map https://play.golang.org/p/OFXhFyyrmZ
>
> On Wednesday, 16 August 2017 03:17:29 UTC+3, Tong Sun wrote:
>>
>> Sorry if this  topic has been discussed, but I'm looking for a Go ordered 
>> map implementation. 
>>
>> I've found 
>>
>> - https://github.com/samdolan/go-ordered-map
>> - https://github.com/emperorcow/orderedmap
>> etc
>>
>> But none seems to be being maintained. 
>>
>> Anyone has used or can recommend a good Go ordered map implementation? 
>>
>> Thx
>>
>>

-- 
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