Giter Club home page Giter Club logo

Comments (6)

flesler avatar flesler commented on May 31, 2024

What you mean sort? It's not an ordered collection.
Are you referring to the order you get them items when you call forEach()?

If so you could do something like:

map.keys().sort().forEach(function(key) {
  var value = map.get(key);
  // ...
});

from hashmap.

joereger avatar joereger commented on May 31, 2024

that'll work... thanks!

from hashmap.

joereger avatar joereger commented on May 31, 2024

so the sort on keys works well... but... how would you go about sorting on value?

from hashmap.

flesler avatar flesler commented on May 31, 2024

This?

map.values().sort().forEach(function(value) {
  // ...
});

from hashmap.

joereger avatar joereger commented on May 31, 2024

Right, but then how to retrieve the key? On this example:

joe : 10
alice : 50
paul : 2
phil : 10

I can sort the values and get 50, 10, 10, 2... but then how do i get back to the keys so i can display something like:

alice : 50
joe : 10
phil : 10
paul 2

Thanks!

from hashmap.

flesler avatar flesler commented on May 31, 2024

Well, for start, there's no point in using using HashMap for string keys. You can use a plain object for that.
At this point, if you wish to continue this way, I'd say you should rather modify hashmap code to suit your needs.

You can solve it by asking for the keys and values and making some sort of external collection but at that point, why would you even be using hashmap.

from hashmap.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.