How can I sort a Map by values ?
33 views (last 30 days)
Show older comments
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);
5 Comments
Adam
on 5 May 2017
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 );
Answers (1)
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, keys_map, values_map] = sort_map(mapObj, order="keys", reverse=1)
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)
1 Comment
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
mapObj0.values
mapObj1 = sort_map(mapObj0); % Default: order="values", reverse=0
mapObj1.keys
mapObj1.values
mapObj2 = sort_map(mapObj0, order="keys", reverse=1);
mapObj2.keys
mapObj2.values
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
See Also
Categories
Find more on Graphics Object Properties in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!