sort string slice like it contains key,val

95 views
Skip to first unread message

Vasiliy Tolstov

unread,
Mar 13, 2021, 1:37:21 PM3/13/21
to golan...@googlegroups.com
Hi!
I'm stuck at sorting stuff like
[]string{"xxxkey","xxxval","zzzkey","zzzval","aaakey","aaaval","zzzkey","ggggval"}
i need to get after sorting something like
[]string{"aaakey","aaaval", "xxxkey","xxxval","zzzkey","ggggval"}

So i'm sort by "key" and if key is duplicated - last wins.
Mostly i want to avoid creating helper slices that contains keys and
vals dedicated, does it possible to do sorting only by swapping
"key/val" ?

--
Vasiliy Tolstov,
e-mail: v.to...@selfip.ru

Levieux Michel

unread,
Mar 13, 2021, 1:44:05 PM3/13/21
to Vasiliy Tolstov, golang-nuts
Hi,

You should take a look at the sort package from the stdlib, it contains an interface that you can easily implement for such problematics :)

Hope this helps!

--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CACaajQt82U878secmTSPaFW85a%3DWA20-vF%2BsPub%2B_w3i%3DohtEA%40mail.gmail.com.

Carla Pfaff

unread,
Mar 13, 2021, 1:47:46 PM3/13/21
to golang-nuts
On Saturday, 13 March 2021 at 14:44:05 UTC+1 mlevi...@gmail.com wrote:
the sort package from the stdlib, it contains an interface that you can easily implement for such problematics :)

Vasiliy Tolstov

unread,
Mar 13, 2021, 8:33:36 PM3/13/21
to Carla Pfaff, golang-nuts
Looks fine =) But how to remove duplicates?
I'm found this stuff
https://github.com/campoy/unique/blob/master/unique.go but as i
understand it needs to adapt to my case =)

сб, 13 мар. 2021 г. в 16:47, 'Carla Pfaff' via golang-nuts
<golan...@googlegroups.com>:
>
> On Saturday, 13 March 2021 at 14:44:05 UTC+1 mlevi...@gmail.com wrote:
>>
>> the sort package from the stdlib, it contains an interface that you can easily implement for such problematics :)
>
>
> Like this: https://play.golang.org/p/eoLJ2aVAWkD
>
>
> --
> 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.
> To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/b8caea5d-fc98-41cf-85aa-b1526105840bn%40googlegroups.com.

Levieux Michel

unread,
Mar 13, 2021, 8:59:49 PM3/13/21
to Vasiliy Tolstov, Carla Pfaff, golang-nuts
Your need is not only to sort your data elements then..? 
Using the previously advised solution you can just range the slice *after* you sort it, so you can just check for the next element, which I'd say is not *too bad*, what are your performance constraints?

Robert Engels

unread,
Mar 13, 2021, 9:42:51 PM3/13/21
to Levieux Michel, Vasiliy Tolstov, Carla Pfaff, golang-nuts
That wasn’t specified in the assignment :)

On Mar 13, 2021, at 2:59 PM, Levieux Michel <mlevi...@gmail.com> wrote:



Vasiliy Tolstov

unread,
Mar 13, 2021, 10:05:12 PM3/13/21
to Levieux Michel, Vasiliy Tolstov, Carla Pfaff, golang-nuts
I think that remove after sort is good for me, thanks

сб, 13 мар. 2021 г. в 23:59, Levieux Michel <mlevi...@gmail.com>:

Vasiliy Tolstov

unread,
Mar 13, 2021, 10:05:41 PM3/13/21
to Robert Engels, Levieux Michel, Vasiliy Tolstov, Carla Pfaff, golang-nuts
вс, 14 мар. 2021 г. в 00:41, Robert Engels <ren...@ix.netcom.com>:
>
> That wasn’t specified in the assignment :)

=) As always after first stuff i need the second =)

Brian Candler

unread,
Mar 13, 2021, 10:10:14 PM3/13/21
to golang-nuts
If I understand rightly, the values in the slice are to be interpreted (key, value) pairs?  In that case, the natural thing to me is to build a map.  This also takes care of "duplicate key, last value wins".  You can then sort the keys and convert it back:

However, I think that a slice of adjacent keys and values is not a particularly natural way to represent this data; it's clearer to make a structure which holds keys and vals.

Vasiliy Tolstov

unread,
Mar 13, 2021, 10:27:52 PM3/13/21
to Brian Candler, golang-nuts
вс, 14 мар. 2021 г. в 01:10, Brian Candler <b.ca...@pobox.com>:
>
> If I understand rightly, the values in the slice are to be interpreted (key, value) pairs? In that case, the natural thing to me is to build a map. This also takes care of "duplicate key, last value wins". You can then sort the keys and convert it back:
> https://play.golang.org/p/dTjmO18T1vQ
>
> However, I think that a slice of adjacent keys and values is not a particularly natural way to represent this data; it's clearer to make a structure which holds keys and vals.
> https://play.golang.org/p/jq358XyKLlx
>

Yes, but in a small amount of items the operation on slice is faster
than map. My case - have not more then 16-20 elements

> On Saturday, 13 March 2021 at 13:37:21 UTC va...@selfip.ru wrote:
>>
>> Hi!
>> I'm stuck at sorting stuff like
>> []string{"xxxkey","xxxval","zzzkey","zzzval","aaakey","aaaval","zzzkey","ggggval"}
>> i need to get after sorting something like
>> []string{"aaakey","aaaval", "xxxkey","xxxval","zzzkey","ggggval"}
>>
>> So i'm sort by "key" and if key is duplicated - last wins.
>> Mostly i want to avoid creating helper slices that contains keys and
>> vals dedicated, does it possible to do sorting only by swapping
>> "key/val" ?
>>
>> --
>> Vasiliy Tolstov,
>> e-mail: v.to...@selfip.ru
>
> --
> 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.
> To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/8eaa86e7-8787-4fc3-bed6-761586825cefn%40googlegroups.com.

Axel Wagner

unread,
Mar 13, 2021, 10:38:23 PM3/13/21
to golang-nuts
One thing I would add is that you'll likely want to use the *Stable versions of the sort functions, to make sure the order of equivalent elements does not change.
Apart from that, the solution posted by Carla above with an added step to remove duplicates seems like the best solution.

Vasiliy Tolstov

unread,
Mar 14, 2021, 10:09:03 AM3/14/21
to Axel Wagner, golang-nuts
вс, 14 мар. 2021 г. в 01:38, 'Axel Wagner' via golang-nuts
<golan...@googlegroups.com>:
>
> One thing I would add is that you'll likely want to use the *Stable versions of the sort functions, to make sure the order of equivalent elements does not change.
> Apart from that, the solution posted by Carla above with an added step to remove duplicates seems like the best solution.
>

Thanks for all help
> To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAEkBMfEpv3MD_AA5SoVGViPxwrW-Tg_h1xbWo7D7tHhFjR88%3DQ%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages