map value low at first
Moderator: Martin
map value low at first
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
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
You can use sort() and getMapValues() here like this.
list=sort(getMapValues(global_test_map));
list=sort(getMapValues(global_test_map));
Re: map value low at first
hi anuraag
Thanks for quick response. one more question. how can add the value 3300 at third place of the map.
from record4
Thanks for quick response. one more question. how can add the value 3300 at third place of the map.
from record4
No.1 Automation app in play store Automagic Premium
Samsung Galaxy j2 non rooted.
Android 5.1.1
Samsung Galaxy j2 non rooted.
Android 5.1.1
Re: map value low at first
You want to replace 7456 with 3300?
addMapEntry(global_test_map, 3, 3300)
Or
global_test_map[3]=3300
addMapEntry(global_test_map, 3, 3300)
Or
global_test_map[3]=3300
Re: map value low at first
hi anuraag
I don't want to replace the value . I want to add one more value at third place.
thanks from record4
I don't want to replace the value . I want to add one more value at third place.
thanks from record4
No.1 Automation app in play store Automagic Premium
Samsung Galaxy j2 non rooted.
Android 5.1.1
Samsung Galaxy j2 non rooted.
Android 5.1.1
Re: map value low at first
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));
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
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
To access any element, example 3rd element (3300), simply use list[2].
Code: Select all
list = newList(4500, 3200, 7456, 1200);
sort(list);
addElement(list, 2, 3300);
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.
Re: map value low at first
I just noticed that sorting negative numbers doesn't work correctly:
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?
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]
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
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.
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];
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.
Re: map value low at first
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.
It works with positive numbers. But again weird results when there are negative numbers. They appear at the end of the temp-list.