Sort dictionary by keypath

Hello everyone :partying_face:

Recently, I needed to sort a dictionary based on a specific keypath, and I thought it might be useful to someone else as well, so I’m sharing with you what I managed to do.

I created two very simple functions.

sortDictionaryByKey


It will sort the dictionary based on its keys, i created this function for completeness, and it’s really very simple.

It is possible, using make new sorted list from, to sort a dictionary as well (not just lists) based on its keys,
But it will be transformed into a list, so we simply convert it back into a dictionary using list of pairs to dictionary pairs, Done!
By setting reverse to true, the dictionary will simply be returned in reverse using reverse list.

Thanks to :raising_hands: dora_paz :raising_hands: I can not sort dictionary with integer key - #9 by dora_paz

sortDictionaryByKeyPath


This function will sort the dictionary based on a keypath.

For example, let’s take a dictionary composed of multiple users with this structure and say we want to sort them based on the first parent’s name.

{
	"user105020266": {
		"name": "Hannah",
		"surname": "Torres",
		"age": 5,
		"randomNumberList": [
			421,
			206,
			993
		],
		"familyComposition": {
			"parents": {
				"parent1": "Bob",
				"parent2": "Madison"
			},
			"siblings": {
				"sibling1": "Olivia",
				"sibling2": "Matthew"
			}
		}
	},
	"user108887936": {
		"name": "Owen",
		"surname": "Gonzalez",
		"age": 93,
		"randomNumberList": [
			502,
			532,
			34
		],
		"familyComposition": {
			"parents": {
				"parent1": "Zoey",
				"parent2": "Charlotte"
			},
			"siblings": {
				"sibling1": "Amelia",
				"sibling2": "Lily",
				"sibling3": "John"
			}
		}
	},
	"user121402250": {
		"name": "John",
		"surname": "Thomas",
		"age": 106,
		"randomNumberList": [
			521,
			264,
			558
		],
		"familyComposition": {
			"parents": {
				"parent1": "Audrey",
				"parent2": "Luna"
			},
			"siblings": {
				"sibling1": "Adrian"
			}
		}
	}
}
  • We will retrieve all the keys of the dictionary
    (user105020266 user108887936 user121402250)
    and save them in keys.
    blocks (8)

  • Then all the values corresponding to the keypath (we can’t use the dictionaries_get_values block because it ignores null elements, so I looped through all the keys to get the value at the keypath)


    I put “z” in case an element is not found so that null values are placed at the end.
    (Bob Zoey Audrey)
    we save in values.

  • Then we will create a new sortedList (list of lists) composed of elements with index 1 being parent1 and index 2 being the dictionary key

(Bob user105020266)
(Zoey user108887936)
(Audrey user121402250)
  • Using make new sorted list from we will sort the sortedList
(Audrey user121402250)
(Bob user105020266)
(Zoey user108887936)

and recreate the dictionary with a loop through this list,


where in each iteration:

  • a variable key will be created, which will be index 2 of each element (user121402250, user105020266, user108887936),
  • a pair will be inserted in sortedDictionary with key key and as value the getValueForKey of the content at that key in the original dictionary.

And as a result, we will pass the new sorted dictionary.

Result

AIA

SortDictionary.aia (10.6 KB)

Happy :kodular:oding!

4 Likes