How do you reorder a list with values stored in a dictionary?

Hello!

I want to know the best way to reorder this list with values stored in a dictionary. I want it to be reordered from greatest to least. This list is used in a list viewer to show the user the most voted item at the top. I've been stumped on how to do this and need some help. If you find a way without a dictionary that's fine!

Here is some code I got to get the basic idea:




This is way too overcomplicated.

Consider a dict where the key is the unique flavor name and the value is its vote count.

Such a dict can be converted to a 2 column table (list of lists) using one of the dict blocks.
dictionaries_dict_to_alist

There are lots of table sorting techniques in

Thank you for the quick response and thoughts!

So from my understanding and quick reading of the docs, that block will be simply 1 dictionary that contains a list of the keys (names) and values (votes) (2 columns chart)? Meaning that I won't have to swift through a list and the many dictionaries.

So this would be more efficient?:

Using the get value block, how would I get the name of the flavor?

  • Edited: Read the docs some more and it's the get value block! (Instead of the get value at key path block)

image
then you will get a list of list (key-value pair)

Thank you for your feedback!

Both ABG and you (Kevinkun) have helped me in the right direction! I did convert it to a list in a list like Kevin said.

Thank you, guys!!

Have you been able to apply a Sort to reorder the values?

Still working on that. I think I have a way but not sure if it will work.

Here is an example sort within the App, blocks-only solution.
LeagueTableSorted_Pts.aia (28.8 KB)

The Project includes the free to use Table View extension v4 by Power User @Ken

The Sort code is by Sajal Dutta
https://imagnity.com/tutorials/app-inventor/list-sorting-on-app-inventor

The sort is of one column. I have made an expanded version to sort by two columns.
LeagueTableSorted_Pts_Gd.aia (27.6 KB)

Hey people!

Thank you guys for helping me improve my code and pointing me in the right direction! I've found a way to order from greatest to least using an extension made by: List Sort Extention @Faraz_Firoz. Then using the unordered votes to match the index of the ordered votes. Though it made duplicates, so I had to remove them by using: Remove Duplicates in a list @Angelo_D.

Here is some updated code:

Here is an image of what it looks like!:

And I'll throw in an image of my thought process. Don't know if this has an actual term for this, if so please let me know!:

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.