How can I sort a Map by values ?
显示 更早的评论
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 个评论
Adam
2017-5-3
containers.Map is an unsorted data structure. You can sort the values and keys if you extract them, but you cannot sort the map itself, it will always return the keys and values ordered alphanumerically by key.
KL
2017-5-3
Thanks for the comment Adam, I removed my answer.
sendja450
2017-5-4
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
2017-5-7
回答(1 个)
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 个评论
"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
类别
在 帮助中心 和 File Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!