Reordering pairs

Hi there,

I was trying to reorder a data set of pairs in the ascending order of the "user" key, but differently from the 'List' component, I could not find any native block there in the Dictionary component with the 'sort' function.

[ { "user":"nameC surnameC", "mobile":"345678"},
{ "user":"nameB surnameB", "mobile":"234567"},
{ "user":"nameA surnameA", "mobile":"123456"},
....
{ "user":"nameX surnameX", "mobile":"456789"} ]

Could someone give insights on that?
Thanks.

I was thinking on something like that, but sounds overly complicated, I wonder if deoesn't have any way without the need of element iterations:

// Step 1: Create a list of dictionaries
Var json = [
{"user": "nameC surnameC", "mobile": "345678"},
{"user": "nameB surnameB", "mobile": "234567"},
{"user": "nameA surnameA", "mobile": "123456"},
{"user": "nameX surnameX", "mobile": "456789"}
];

// Step 2: Sort the list based on the "user" key
// Sort in ascending order
Sort list in place json by key "user" ascending

// Step 3: Rebuild the dictionary from the sorted list
Var sortedJson = create empty list
For each item in json
Add item to list sortedJson

// Now, sortedJson contains the sorted list of dictionaries based on the "user" key.

You could use javascript....

1 Like

There is a list block near the bottom of the list block pallet to sort a list based on a customized comparison, item1 versus item2.

Just compare the values of the user key.

1 Like

some like this:

2 Likes

The above block worked like a charm, thank all you for your inputs.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.