Sign in

How does golang return data of an ordered map type

happylucklink edited in Wed, 19 Oct 2022

We know that golang's map is unordered, but in practical application, I often need to return an ordered format, such as a list sorted by time group. How to implement this idea

6 Replies
commented on Wed, 19 Oct 2022

There are two scenarios: 1. In your own program, you should sort according to the sub segments of the structure as much as possible, and you can use the sort interface to customize the structure sorting rules. 2. If it is due to business and interaction with other modules, the map must be sorted to return an ordered JSON string, you can consider starting with serialization. If you want to save trouble, you can directly use the third-party library github.com/iancoleman/orderedmap

commented on Wed, 19 Oct 2022

Go's sort package provides an interface

type Interface interface {
        // Len is the number of elements in the collection.
        Len() int
        // Less reports whether the element with
        // index i should sort before the element with index j.
        Less(i, j int) bool
        // Swap swaps the elements with indexes i and j.
        Swap(i, j int)

Just implement the method according to the required sorting rules. For example, what you said in the title needs to be sorted according to the order of time, so the implementation of less method is to compare the size with the time field of the object.

Here's a demo, sorted by age.

package main

import (

// Person struct
type Person struct {
    Name string
    Age  int 

// Persons a set of person
type Persons []Person

// Len return count
func (p Persons) Len() int {
    return len(p)

// Less return bigger true
func (p Persons) Less(i, j int) bool {
    return p[i].Age < p[j].Age                                                                                                                                                           

// Swap swap items
func (p Persons) Swap(i, j int) {
    p[i], p[j] = p[j], p[i]

func main() {
    ps := Persons{}
    ps = append(ps, Person{
        "张三", 31, 
    ps = append(ps, Person{
        "李四", 23, 
    ps = append(ps, Person{
        "王五", 40, 

commented on Wed, 19 Oct 2022

Referring to Java treemap, the node of map contains a pointer to string all elements in an orderly manner

commented on Wed, 19 Oct 2022

Use the array or linked list to record the order of the map, traverse according to the array or linked list.

commented on Thu, 20 Oct 2022

Map and list together, the disadvantage is to have additional application space, specific problems specific treatment

commented on Thu, 20 Oct 2022

Combine @ Andy_ spf @__ If you sort according to the key of the map: key into slice, and then sort slice (sort package), and then map with the help of sorted slice output in order, reference: link description

2. If Val is sorted according to Val, and val itself is a complex data structure, such as struct, the idea is: construct kV as struct, then implement sort of struct (implement the interface of sort package), and traverse the output of struct

[email protected]_ The summary of the SPF boss is in place. Where can the GitHub project be used directly (after understanding the code idea)