Page 1 of 2
map value low at first
Posted: 06 Jul 2019 08:38
by Rafi4
Hi
how can I get the value of a map of low value as described below example
map =global_test_map
key value
1 4500
2 3200
3 7456
4 1200
in above mentioned list how can I get 1200 ?after getting the value 3200,4500,7456. so I want to get one by one.
thanks from record4
Re: map value low at first
Posted: 06 Jul 2019 11:30
by anuraag
You can use sort() and getMapValues() here like this.
list=sort(getMapValues(global_test_map));
Re: map value low at first
Posted: 06 Jul 2019 11:39
by Rafi4
hi anuraag
Thanks for quick response. one more question. how can add the value 3300 at third place of the map.
from record4
Re: map value low at first
Posted: 06 Jul 2019 11:44
by anuraag
You want to replace 7456 with 3300?
addMapEntry(global_test_map, 3, 3300)
Or
global_test_map[3]=3300
Re: map value low at first
Posted: 06 Jul 2019 12:17
by Rafi4
hi anuraag
I don't want to replace the value . I want to add one more value at third place.
thanks from record4
Re: map value low at first
Posted: 06 Jul 2019 13:23
by anuraag
It will be tricky to add key value at specific index to map. It will be better to use two lists instead.
example
keys=newList(1,2,3,4);
values=newList(4500,3200,7456,1200);
To add an entry at 3rd place. We will use 2 here as index as index starts from 0
addElement(keys, 2, key),;
addElement(keys, 2, value),;
To get value of key 3
index=indexOfElement(keys, 3);
value=getElement(values, index);
To get sorted values from lower to high
Use copyList() first as here if you sort directly list then you will get wrong values for key.
sortedList=sort(copyList(values));
Re: map value low at first
Posted: 06 Jul 2019 17:21
by Desmanto
It seems you simply to maintain the value part of the map. Maybe you don't need the key 1,2,3,4 at all. Simply use
Code: Select all
list = newList(4500, 3200, 7456, 1200);
sort(list);
addElement(list, 2, 3300);
To access any element, example 3rd element (3300), simply use list[2].
Re: map value low at first
Posted: 28 Jul 2019 17:05
by Horschte
I just noticed that sorting negative numbers doesn't work correctly:
Code: Select all
list = newList(4500, 3200, 7456, 1200, 500, -1000, -2000);
sort(list);
//result [-1000, -2000, 500, 1200, 3200, 4500, 7456]
//expected result [-2000, -1000, 500, 1200, 3200, 4500, 7456]
Am I wrong?
And to get back to the example in the first post of this thread:
How can I sort a map without losing the keys?
Re: map value low at first
Posted: 28 Jul 2019 18:26
by Desmanto
Can confirm the bug, I get the same wrong result as yours.
To sort map value while maintaining the key, you have to convert it temporary to nested list with the value as the first element in each nest, sort it, then convert back to map.
Code: Select all
map = newMapFromValues(
1, 4500,
2, 3200,
3, 7456,
4, 1200 );
//convert temporary to nested list
temp = newList();
for(i in getMapKeys(map))
addElement(temp, newList(map[i], i));
sort(temp);
//create new map from sorted list
sortedmap = newMap();
for(i in temp)
sortedmap[i[1]] = i[0];
Re: map value low at first
Posted: 28 Jul 2019 20:16
by Horschte
Perfect. Thank you.
It works with positive numbers. But again weird results when there are negative numbers. They appear at the end of the temp-list.