How can I sort a Map by values ?

조회 수: 53 (최근 30일)
sendja450
sendja450 2017년 5월 3일
편집: Stephen23 2023년 4월 11일
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월 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일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Graphics Object Properties에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by