H
H
Hanry6542021-05-20 23:41:15
Node.js
Hanry654, 2021-05-20 23:41:15

How to sort map by numbers (largest to smallest)?

60a6c926370f9662505512.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Aetae, 2021-05-20
@Hanry654

First: if your keys are strings - use an object without a prototype ( ) instead - working with it is an order of magnitude faster. Object.create(null)Map
//upd: as Dmitry Belyaev rightly noted , in modern browsers the performance of Map is not less than Object.

Second: the order of the keys in Mapcorresponds to the order of addition and cannot be changed after the fact. To sort, you should get from Mapan array, sort, and either create a new one Mapfrom it, or re-add all the values ​​​​to the original one, having previously cleared it.
If the keys are known in advance, then it is better to prepare Mapin advance with empty values ​​- overwriting does not change the order.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question