How can I arrange a datamap like (dm:'a',1,'b',5,'c',2)

I want to arrange a datamap with a internal structure like (dm:‘string’,number)

And the only way I came up with is the (for:) loop way like below


(set:$sample to (dm:
'a',1,
'b',5,
'c',2
))
(set:$newsample to (dm:))
(set:$finalsample to (dm:))




(for: each _i,...(datanames:$sample) )[

(set:$newsample to it + (dm:$sample's (_i),_i))

]

(set:$arrange to(sorted:...(datanames:$newsample)))


(for:each _i,...$arrange)[

(set:$finalsample to it +(dm: $newsample's (_i),_i))

]

(print:$bb)

basically I revert the structure of (dm:‘string’,number) to (dm: number,‘string’)

and use the (sorted:) marco to sort the (datanames:) of new datamap, and then revert it back

But I kind thinks it might not a little slower, So is ther a better way to do the trick?

just like the (sorted) marco to arrays

Thanks!

  (set:$sample to (dm:
'a',2,
'b',5,
'c',2
))

I just realize it wont work because if the structure is (dm: number,‘string’) like above,
then if it has a repeat number 2, then the data value would override the previous one

NOW I don’t know how to do compeletely

A little background…

Current the (datamap:) macro internally uses a JavaScript Map object to store the name / value pairs passed it, and that collection data-type stores those pairs in the order they were added to it.

The Name component of each name / value pair added to a datamap is meant to be a String value, which is likely why your (dm: number,'string') code didn’t work as expected.

As explained in the (datanames:) macro’s documentation, the Array of Names returned by the macro are always sorted alphabetically. The following example demonstrates this behaviour…

(set: $datamap to (dm: 'a', 1, 'd', 2, 'c', 3, 'b', 4))
datamap: (print: $datamap)
names: (print: (datanames: $datamap))

Your question is a little unclear about what exactly you’re trying to achieve… are you trying to sort the property names of the original datamap based on the Number value associated with each? And if so what is meant to happen when two or more properties have the same numerical value?

Hi,Greyelf

Yes I intend to sort the property names of the original datamap based on the Number value associated with each

I actually want to create a billboard , say 3 person, (bill,5, mike,6, john,7)

each has it’s own vote, and I made a process to automatically add vote to each person

and finally I want to arrange the billboard by there votes

after learning your explanation, I tried to (str:)numbers so they can be used as datanames

so now If I (print:$sampe ), it can achieve what I want ,but If I only want to choose the first 3 people, I cant, since the (dataname :slight_smile: would rearrange the names alphabetically .

if 2 people has the same vote, I think I can let harlowe decide who came first

Thanks!!