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 8909317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:23:58+00:00 2026-06-15T03:23:58+00:00

I am interested in using a sorted map in groovy (with gremlin which is

  • 0

I am interested in using a sorted map in groovy (with gremlin which is a DSL for graph databases).

I have looked at this blog post on sorted maps here, but I am still a bit confused.

  • How are sorted maps declared? Is it any different from the standard way for maps y = [:]?

  • When using a sorted map, are items inserted into the list going to be in the order they are inserted? Or will I have to run sort{} before the items in the sorted map are sorted?

  • 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-15T03:23:59+00:00Added an answer on June 15, 2026 at 3:23 am

    If you just declare a map like so:

    def m = [:]
    

    Then, you can see Groovy by default makes a LinkedHashMap

    assert m.getClass().name == 'java.util.LinkedHashMap'
    

    If you look at the documentation for LinkedHashMap it says:

    Hash table and linked list implementation of the Map interface, with predictable iteration order. This implementation differs from HashMap in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order).

    So LinkedHashMap has an order, and you can affect that order in Groovy by calling sort

    def m = [ b:1, a:2 ]
    
    // Sort by descending value
    m = m.sort { -it.value }
    
    println m // prints [a:2, b:1]
    

    If you want natural ordering of the keys, then you can use one of Java’s sorted maps, such as TreeMap

    To Say you want to use this in Groovy, you can do:

    // def tm = [ tim_yates:1, F21:2 ] as TreeMap // works as well
    TreeMap tm = [ tim_yates:1, F21:2 ]
    

    Then printing this, you can see it is ordered by the keys:

    println map // prints [F21:b, tim_yates:a]
    

    A TreeMap will maintain order as you add keys. A LinkedHashMap will not automatically remain sorted when you add new values.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am interested in using a DataObject and have been looking through the documentation.
I'm interested in using fcbkcomplete , which has: onselect – fire event on item
I'm interested in using bootstrap for a blog, and I've seen a few other
Interested in using Sphinx for my application. Planning to install their latest version which
I currently have an array of pair<double, int> which I sort using a simple
I'm interested in using the facebook graph api to pass an email and obtain
I'm interested in using Flash or Flex to create a File Explorer/Tree which the
I am interested in using a class file (CameraPreview.java) from this package com.example.android.apis.graphics. I
I'm interested in using an Object-Relational Mapping package for an upcoming project. This project
I am interested in using/learning RoR in a project where I have to use

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.