How to prevent default sorting of map

233 views
Skip to first unread message

Vejju Deepesh

unread,
May 30, 2022, 1:28:09 PM5/30/22
to golang-nuts
I want know the method of preventing sorting by default and maintain the map in the order of insertion

Ian Lance Taylor

unread,
May 30, 2022, 1:31:30 PM5/30/22
to Vejju Deepesh, golang-nuts
On Mon, May 30, 2022 at 10:28 AM Vejju Deepesh <vejjud...@gmail.com> wrote:
>
> I want know the method of preventing sorting by default and maintain the map in the order of insertion

The builtin map type does not support that. Sorry.

Ian

Dan Kortschak

unread,
May 30, 2022, 6:34:02 PM5/30/22
to golan...@googlegroups.com
On Mon, 2022-05-30 at 08:23 -0700, Vejju Deepesh wrote:
> I want know the method of preventing sorting by default and maintain
> the map in the order of insertion

If you want ordered return of elements and O(1) look-up, use a slice
and an index map. Insertion becomes and appends and a map insertion
with the length of the slice after append (if the element is not
already in the map - no-op otherwise), look-up is an index map query
and then slice access, and container dump is an iteration over the
slice.

Reply all
Reply to author
Forward
0 new messages