map value low at first

Post your questions and help other users.

Moderator: Martin

User avatar
Rafi4
Posts: 281
Joined: 01 Dec 2017 05:23

map value low at first

Post by Rafi4 » 06 Jul 2019 08:38

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

anuraag
Posts: 371
Joined: 24 Jan 2015 02:06

Re: map value low at first

Post by anuraag » 06 Jul 2019 11:30

You can use sort() and getMapValues() here like this.
list=sort(getMapValues(global_test_map));

User avatar
Rafi4
Posts: 281
Joined: 01 Dec 2017 05:23

Re: map value low at first

Post by Rafi4 » 06 Jul 2019 11:39

hi anuraag
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

anuraag
Posts: 371
Joined: 24 Jan 2015 02:06

Re: map value low at first

Post by anuraag » 06 Jul 2019 11:44

You want to replace 7456 with 3300?

addMapEntry(global_test_map, 3, 3300)

Or

global_test_map[3]=3300

User avatar
Rafi4
Posts: 281
Joined: 01 Dec 2017 05:23

Re: map value low at first

Post by Rafi4 » 06 Jul 2019 12:17

hi anuraag
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

anuraag
Posts: 371
Joined: 24 Jan 2015 02:06

Re: map value low at first

Post by anuraag » 06 Jul 2019 13:23

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));

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: map value low at first

Post by Desmanto » 06 Jul 2019 17:21

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].
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.

Horschte
Posts: 56
Joined: 03 Nov 2014 18:00

Re: map value low at first

Post by Horschte » 28 Jul 2019 17:05

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?

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: map value low at first

Post by Desmanto » 28 Jul 2019 18:26

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];
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.

Horschte
Posts: 56
Joined: 03 Nov 2014 18:00

Re: map value low at first

Post by Horschte » 28 Jul 2019 20:16

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.

Post Reply