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
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
KL 2017 年 5 月 3 日
Thanks for the comment Adam, I removed my answer.
sendja450
sendja450 2017 年 5 月 4 日
thanks for your comments, if I want to extract theme I will lose the (key, value) property. So how can I extract in a for loop the max in each iteration in order to have a descending order ?
Adam
Adam 2017 年 5 月 5 日
編集済み: Adam 2017 年 5 月 8 日
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
sendja450 2017 年 5 月 7 日
Thanks for your help @Adam.

サインインしてコメントする。

回答 (1 件)

KUNHUAN
KUNHUAN 2023 年 4 月 11 日

0 投票

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)

1 件のコメント

Stephen23
Stephen23 2023 年 4 月 11 日
編集済み: Stephen23 2023 年 4 月 11 日
"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

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeShifting and Sorting Matrices についてさらに検索

質問済み:

2017 年 5 月 3 日

編集済み:

2023 年 4 月 11 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by