# How can I sort a Map by values ?

35 views (last 30 days)
sendja450 on 3 May 2017
Edited: Stephen23 on 11 Apr 2023
For the example below, how can I sort the mapObj by values and of course the keys must also folow the valeus new order.
keySet = {'Jan', 'Feb', 'Mar', 'Apr'};
valueSet = [327.2, 368.2, 197.6, 178.4];
mapObj = containers.Map(keySet,valueSet);
Edited: Adam on 8 May 2017
You can extract the keys and the values and do a sort on the keys, making sure to retain the 2nd output argument from sort - these are the indices required to apply to the values in order for them to be ordered to match the keys still e.g.
keys = myMap.keys;
values = myMap.values;
[sortedKeys, sortIdx] = sort( keys );
sortedValues = values( sortIdx );
sendja450 on 7 May 2017

KUNHUAN on 11 Apr 2023
IDEA
One way is to retrieve keys and values and sort them independently.
First, sort values. Then, sort keys. Lastly, reassign keys and values.
Using flip to sort in reverse order.
IMPLEMENTATION
keySet = {'Jan', 'Feb', 'Mar', 'Apr'};
valueSet = [327.2, 368.2, 197.6, 178.4];
mapObj = containers.Map(keySet,valueSet);
[mapObj, keys_map, values_map] = sort_map(mapObj) % Default: order="values", reverse=0
mapObj =
Map with properties: Count: 4 KeyType: char ValueType: double
keys_map = 1×4 cell array
{'Apr'} {'Mar'} {'Jan'} {'Feb'}
values_map = 1×4
178.4000 197.6000 327.2000 368.2000
[mapObj, keys_map, values_map] = sort_map(mapObj, order="keys", reverse=1)
mapObj =
Map with properties: Count: 4 KeyType: char ValueType: double
keys_map = 1×4 cell array
{'Mar'} {'Jan'} {'Feb'} {'Apr'}
values_map = 1×4
197.6000 327.2000 368.2000 178.4000
Define the following sort_map function.
function [output, keys_map, values_map] = sort_map (input, options)
arguments
input containers.Map;
options.order char = "values";
options.reverse logical = 0;
end
k = keys(input);
v = cell2mat(values(input));
% Default return
output = input;
keys_map = k;
values_map = v;
if (options.order == "keys")
[k_new, order] = sort(k);
if (options.reverse == 1)
k_new = flip(k_new);
order = flip(order);
end
v_new = v(order);
output = containers.Map(k_new, v_new);
keys_map = k_new;
values_map = v_new;
elseif (options.order == "values")
[v_new, order] = sort(v);
if (options.reverse == 1)
v_new = flip(v_new);
order = flip(order);
end
k_new = k(order);
output = containers.Map(k_new, v_new);
keys_map = k_new;
values_map = v_new;
end
end
REFERENCES
There is a section that introduces sorting with the same order:
% X = [3 6 4 2 1 5];
% Y = ["yellow" "purple" "green" "orange" "red" "blue"];
% [Xsorted,I] = sort(X)
% Ysorted = Y(I)
Stephen23 on 11 Apr 2023
Edited: Stephen23 on 11 Apr 2023
"Lastly, reassign keys and values" does nothing.
As Adam explained six years ago here, containers.Map is an unsorted data type, so this code makes absolutely no difference to the order of the keys and values of the containers.Map objects themselves:
keySet = {'Jan', 'Feb', 'Mar', 'Apr'};
valueSet = [327.2, 368.2, 197.6, 178.4];
mapObj0 = containers.Map(keySet,valueSet);
mapObj0.keys
ans = 1×4 cell array
{'Apr'} {'Feb'} {'Jan'} {'Mar'}
mapObj0.values
ans = 1×4 cell array
{[178.4000]} {[368.2000]} {[327.2000]} {[197.6000]}
mapObj1 = sort_map(mapObj0); % Default: order="values", reverse=0
mapObj1.keys
ans = 1×4 cell array
{'Apr'} {'Feb'} {'Jan'} {'Mar'}
mapObj1.values
ans = 1×4 cell array
{[178.4000]} {[368.2000]} {[327.2000]} {[197.6000]}
mapObj2 = sort_map(mapObj0, order="keys", reverse=1);
mapObj2.keys
ans = 1×4 cell array
{'Apr'} {'Feb'} {'Jan'} {'Mar'}
mapObj2.values
ans = 1×4 cell array
{[178.4000]} {[368.2000]} {[327.2000]} {[197.6000]}
function [output, keys_map, values_map] = sort_map (input, options)
arguments
input containers.Map;
options.order char = "values";
options.reverse logical = 0;
end
k = keys(input);
v = cell2mat(values(input));
% Default return
output = input;
keys_map = k;
values_map = v;
if (options.order == "keys")
[k_new, order] = sort(k);
if (options.reverse == 1)
k_new = flip(k_new);
order = flip(order);
end
v_new = v(order);
output = containers.Map(k_new, v_new);
keys_map = k_new;
values_map = v_new;
elseif (options.order == "values")
[v_new, order] = sort(v);
if (options.reverse == 1)
v_new = flip(v_new);
order = flip(order);
end
k_new = k(order);
output = containers.Map(k_new, v_new);
keys_map = k_new;
values_map = v_new;
end
end