# How to sort a map with values

9 replies
pensee
Joined: 2011-10-21,

import scala.collection.mutable.HashMap
val treasureMap =HashMap("m5" -> 0.5, "m2" -> 0.3,"m1" -> 0.2, "m3" ->
0.5, "m4" -> 0.3)

What I want to get is another HashMap as ("m3" -> 0.5, "m5" ->
0.5,"m2" -> 0.3, "m4" -> 0.3, "m1" -> 0.2)

Anybody knows how to do this?
Thanks!

Alec Zorab
Joined: 2010-05-18,
Re: How to sort a map with values

The ordering on a hashmap is defined by the hashing funtion. If you
just mean you want the pairs sorted by the second element, try

treasureMap.toSeq.sortBy(_._2)

On 4 January 2012 16:01, yannick wrote:
> import scala.collection.mutable.HashMap
> val treasureMap =HashMap("m5" -> 0.5, "m2" -> 0.3,"m1" -> 0.2, "m3" ->
> 0.5, "m4" -> 0.3)
>
> What I want to get is another HashMap as ("m3" -> 0.5, "m5" ->
> 0.5,"m2" -> 0.3, "m4" -> 0.3, "m1" -> 0.2)
>
> Anybody knows how to do this?
> Thanks!
>

ichoran
Joined: 2009-08-14,
Re: How to sort a map with values
...sortBy(- _._2) if you want highest values first.
--Rex

On Wed, Jan 4, 2012 at 11:14 AM, Alec Zorab <aleczorab [at] googlemail [dot] com> wrote:
The ordering on a hashmap is defined by the hashing funtion. If you
just mean you want the pairs sorted by the second element, try

treasureMap.toSeq.sortBy(_._2)

On 4 January 2012 16:01, yannick <yannick [dot] crystal [at] gmail [dot] com> wrote:
> import scala.collection.mutable.HashMap
> val treasureMap =HashMap("m5" -> 0.5, "m2" -> 0.3,"m1" -> 0.2, "m3" ->
> 0.5, "m4" -> 0.3)
>
> What I want to get is another HashMap as ("m3" -> 0.5, "m5" ->
> 0.5,"m2" -> 0.3, "m4" -> 0.3, "m1" -> 0.2)
>
> Anybody knows how to do this?
> Thanks!
>

H-star Development
Joined: 2010-04-14,
Re: How to sort a map with values

new LinkedHashMap ++ oldMap.toSeq.sortBy(...)

or use a treemap

Am 04.01.2012 17:01, schrieb yannick:
> import scala.collection.mutable.HashMap
> val treasureMap =HashMap("m5" -> 0.5, "m2" -> 0.3,"m1" -> 0.2, "m3" ->
> 0.5, "m4" -> 0.3)
>
> What I want to get is another HashMap as ("m3" -> 0.5, "m5" ->
> 0.5,"m2" -> 0.3, "m4" -> 0.3, "m1" -> 0.2)
>
> Anybody knows how to do this?
> Thanks!
>
>

dcsobral
Joined: 2009-04-23,
Re: How to sort a map with values

On Wed, Jan 4, 2012 at 14:01, yannick wrote:
> import scala.collection.mutable.HashMap
> val treasureMap =HashMap("m5" -> 0.5, "m2" -> 0.3,"m1" -> 0.2, "m3" ->
> 0.5, "m4" -> 0.3)
>
> What I want to get is another HashMap as ("m3" -> 0.5, "m5" ->
> 0.5,"m2" -> 0.3, "m4" -> 0.3, "m1" -> 0.2)
>
> Anybody knows how to do this?

Use a SortedMap instead.

Kevin Wright 2
Joined: 2010-05-30,
Re: How to sort a map with values
TreeMap is the only subclass of the SortedMap trait, so they're the same thing :)

On 4 January 2012 17:35, Daniel Sobral <dcsobral [at] gmail [dot] com> wrote:
On Wed, Jan 4, 2012 at 14:01, yannick <yannick [dot] crystal [at] gmail [dot] com> wrote:
> import scala.collection.mutable.HashMap
> val treasureMap =HashMap("m5" -> 0.5, "m2" -> 0.3,"m1" -> 0.2, "m3" ->
> 0.5, "m4" -> 0.3)
>
> What I want to get is another HashMap as ("m3" -> 0.5, "m5" ->
> 0.5,"m2" -> 0.3, "m4" -> 0.3, "m1" -> 0.2)
>
> Anybody knows how to do this?

Use a SortedMap instead.

--
Daniel C. Sobral

I travel to the future all the time.

--
Kevin Wright
mail: kevin [dot] wright [at] scalatechnology [dot] com
gtalk / msn : kev [dot] lee [dot] wright [at] gmail [dot] com quora: http://www.quora.com/Kevin-Wrightgoogle+: http://gplus.to/thecoda
kev [dot] lee [dot] wright [at] gmail [dot] com twitter: @thecoda
vibe / skype: kev.lee.wrightsteam: kev_lee_wright
"My point today is that, if we wish to count lines of code, we should not regard them as "lines produced" but as "lines spent": the current conventional wisdom is so foolish as to book that count on the wrong side of the ledger" ~ Dijkstra
nilskp
Joined: 2009-01-30,
Re: How to sort a map with values
On Wed, Jan 4, 2012 at 11:35 AM, Daniel Sobral <dcsobral [at] gmail [dot] com> wrote:
On Wed, Jan 4, 2012 at 14:01, yannick <yannick [dot] crystal [at] gmail [dot] com> wrote:
> import scala.collection.mutable.HashMap
> val treasureMap =HashMap("m5" -> 0.5, "m2" -> 0.3,"m1" -> 0.2, "m3" ->
> 0.5, "m4" -> 0.3)
>
> What I want to get is another HashMap as ("m3" -> 0.5, "m5" ->
> 0.5,"m2" -> 0.3, "m4" -> 0.3, "m1" -> 0.2)
>
> Anybody knows how to do this?

Use a SortedMap instead.

He wants to sort the values, not the keys

--
Daniel C. Sobral

I travel to the future all the time.

dcsobral
Joined: 2009-04-23,
Re: How to sort a map with values

On Wed, Jan 4, 2012 at 15:47, Kevin Wright wrote:
> TreeMap is the only subclass of the SortedMap trait, so they're the same
> thing :)

I saw no TreeMap being used, and I still don't see it.

>
>
>
> On 4 January 2012 17:35, Daniel Sobral wrote:
>>
>> On Wed, Jan 4, 2012 at 14:01, yannick wrote:
>> > import scala.collection.mutable.HashMap
>> > val treasureMap =HashMap("m5" -> 0.5, "m2" -> 0.3,"m1" -> 0.2, "m3" ->
>> > 0.5, "m4" -> 0.3)
>> >
>> > What I want to get is another HashMap as ("m3" -> 0.5, "m5" ->
>> > 0.5,"m2" -> 0.3, "m4" -> 0.3, "m1" -> 0.2)
>> >
>> > Anybody knows how to do this?
>>
>> Use a SortedMap instead.
>>
>>
>> --
>> Daniel C. Sobral
>>
>> I travel to the future all the time.
>
>
>
>
> --
> Kevin Wright
> mail: kevin [dot] wright [at] scalatechnology [dot] com
> gtalk / msn : kev [dot] lee [dot] wright [at] gmail [dot] com
> quora: http://www.quora.com/Kevin-Wright
> vibe / skype: kev.lee.wright
> steam: kev_lee_wright
>
> "My point today is that, if we wish to count lines of code, we should not
> regard them as "lines produced" but as "lines spent": the current
> conventional wisdom is so foolish as to book that count on the wrong side of
> the ledger" ~ Dijkstra
>

dcsobral
Joined: 2009-04-23,
Re: How to sort a map with values

On Wed, Jan 4, 2012 at 16:29, Nils Kilden-Pedersen wrote:
> On Wed, Jan 4, 2012 at 11:35 AM, Daniel Sobral wrote:
>>
>> On Wed, Jan 4, 2012 at 14:01, yannick wrote:
>> > import scala.collection.mutable.HashMap
>> > val treasureMap =HashMap("m5" -> 0.5, "m2" -> 0.3,"m1" -> 0.2, "m3" ->
>> > 0.5, "m4" -> 0.3)
>> >
>> > What I want to get is another HashMap as ("m3" -> 0.5, "m5" ->
>> > 0.5,"m2" -> 0.3, "m4" -> 0.3, "m1" -> 0.2)
>> >
>> > Anybody knows how to do this?
>>
>> Use a SortedMap instead.
>
>
> He wants to sort the values, not the keys

Oops... yes, there's that.

H-star Development
Joined: 2010-04-14,
Re: How to sort a map with values
you can use a comparator that takes the value to both keys and compares them

Am 04.01.2012 19:29, schrieb Nils Kilden-Pedersen:
CABDULvUShcZW-DwRi9qqyC833F6_-W9EZnpK2zR0uoMZ4ULdRg [at] mail [dot] gmail [dot] com" type="cite"> On Wed, Jan 4, 2012 at 11:35 AM, Daniel Sobral <dcsobral [at] gmail [dot] com" rel="nofollow">dcsobral [at] gmail [dot] com> wrote:
On Wed, Jan 4, 2012 at 14:01, yannick <yannick [dot] crystal [at] gmail [dot] com" rel="nofollow">yannick [dot] crystal [at] gmail [dot] com> wrote:
> import scala.collection.mutable.HashMap
> val treasureMap =HashMap("m5" -> 0.5, "m2" -> 0.3,"m1" -> 0.2, "m3" ->
> 0.5, "m4" -> 0.3)
>
> What I want to get is another HashMap as ("m3" -> 0.5, "m5" ->
> 0.5,"m2" -> 0.3, "m4" -> 0.3, "m1" -> 0.2)
>
> Anybody knows how to do this?

Use a SortedMap instead.

He wants to sort the values, not the keys

--
Daniel C. Sobral

I travel to the future all the time.

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland