Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8427867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:53:47+00:00 2026-06-10T04:53:47+00:00

Why is the map printing out of order, and how do I get it

  • 0

Why is the map printing out of order, and how do I get it in to order?

package main

import (
    "fmt"
)

type monthsType struct {
    no   int
    text string
}

var months = map[int]string{
    1:"January", 2:"Fabruary", 3:"March", 4:"April", 5:"May", 6:"June",
    7:"July", 8:"August", 9:"September", 10:"October", 11:"Novenber", 12:"December",
}

func main(){
    for no, month := range months {
        fmt.Print(no)
        fmt.Println("-" + month)
    }
}

Prints out:

10-October
7-July
1-January
9-September
4-April
5-May
2-Fabruary
12-December
11-Novenber
6-June
8-August
3-March
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T04:53:49+00:00Added an answer on June 10, 2026 at 4:53 am

    Code:

    func DemoSortMap() (int, error) {
        fmt.Println("use an array to access items by number:")
        am := [2]string{"jan", "feb"}
        for i, n := range am {
            fmt.Printf("%2d: %s\n", i, n)
        }
        fmt.Println("maps are non-sorted:")
        mm := map[int]string{2: "feb", 1: "jan"}
        for i, n := range mm {
            fmt.Printf("%2d: %s\n", i, n)
        }
        fmt.Println("access items via sorted list of keys::")
        si := make([]int, 0, len(mm))
        for i := range mm {
            si = append(si, i)
        }
        sort.Ints(si)
        for _, i := range si {
            fmt.Printf("%2d: %s\n", i, mm[i])
        }
    
        return 0, nil
    }
    

    (most of it stolen from M. Summerfield’s book)

    output:

    use an array to access items by number:
     0: jan
     1: feb
    maps are non-sorted:
     2: feb
     1: jan
    access items via sorted list of keys::
     1: jan
     2: feb
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Map<String, List<String>> words = new HashMap<String, List<String>>(); List<Map> listOfHash = new ArrayList<Map>(); for (int
I want to define a generic function for printing contents of std::map like types.
Map<Integer, Map<String, String>> movie = new HashMap<Integer,Map<String, String>>(); Map<String,String> sub = new HashMap<String,String>(); I'm
Map Reduce is a pattern that seems to get a lot of traction lately
map.root :controller => main, :action => index is not redirecting the main controller to
My map is defined as such: map<string, LocationStruct> myLocations; where the key is a
If I have following map: NavigableMap<Integer,String> nmap = new TreeMap<Integer,String>(); nmap.put(3, Three); nmap.put(1, One);
I'm just starting to work with the Java Map data type and I was
Let's say I got a Map<String, String> and I wanna remove all the entries
Doing some work on controlling printing via the System.Printing framework (main classes are PrintQueue,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.