Re: [go-nuts] Re: sort string slice like it contains key,val
вс, 14 мар. 2021 г. в 01:38, 'Axel Wagner' via 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. > Thanks for all help > On Sat, Mar 13, 2021 at 11:27 PM Vasiliy Tolstov wrote: >> >> вс, 14 мар. 2021 г. в 01:10, Brian Candler : >> > >> > 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","val"} >> >> i need to get after sorting something like >> >> []string{"aaakey","aaaval", "xxxkey","xxxval","zzzkey","val"} >> >> >> >> 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+unsubscr...@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. >> >> >> >> -- >> Vasiliy Tolstov, >> e-mail: v.tols...@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+unsubscr...@googlegroups.com. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/golang-nuts/CACaajQvwWX3N0tczs31Jka%3D2UDD_kK2SN-QYJTS_eO46cLAyuQ%40mail.gmail.com. > > -- > 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. > To view this discussion on the web visit > https://groups.google.com/d/msgid/golang-nuts/CAEkBMfEpv3MD_AA5SoVGViPxwrW-Tg_h1xbWo7D7tHhFjR88%3DQ%40mail.gmail.com. -- Vasiliy Tolstov, e-mail: v.tols...@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+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CACaajQvW9eaTDEFGgwSDN-vb9UMk1NMis68VBKf4k-w_%3Dw_DuQ%40mail.gmail.com.
Re: [go-nuts] Re: sort string slice like it contains key,val
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. On Sat, Mar 13, 2021 at 11:27 PM Vasiliy Tolstov wrote: > вс, 14 мар. 2021 г. в 01:10, Brian Candler : > > > > 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","val"} > >> i need to get after sorting something like > >> []string{"aaakey","aaaval", "xxxkey","xxxval","zzzkey","val"} > >> > >> 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+unsubscr...@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 > . > > > > -- > Vasiliy Tolstov, > e-mail: v.tols...@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+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/golang-nuts/CACaajQvwWX3N0tczs31Jka%3D2UDD_kK2SN-QYJTS_eO46cLAyuQ%40mail.gmail.com > . > -- 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. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAEkBMfEpv3MD_AA5SoVGViPxwrW-Tg_h1xbWo7D7tHhFjR88%3DQ%40mail.gmail.com.
Re: [go-nuts] Re: sort string slice like it contains key,val
вс, 14 мар. 2021 г. в 01:10, Brian Candler : > > 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","val"} >> i need to get after sorting something like >> []string{"aaakey","aaaval", "xxxkey","xxxval","zzzkey","val"} >> >> 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+unsubscr...@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. -- Vasiliy Tolstov, e-mail: v.tols...@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+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CACaajQvwWX3N0tczs31Jka%3D2UDD_kK2SN-QYJTS_eO46cLAyuQ%40mail.gmail.com.