# Sorting listpicker items

Hi, how do I start to sort a listpicker list I placed an unsorted list into a tinydb and then transfered them to a listpicker (see blocks) the listpicker displays 4,5,5+5-,6,6+,6-,7,7+,7-,8,8+8- I need 4,5-,5,5+,6-,6,6+,7-,7-7+,8-,8,8+

I tried the sortlist extension but this does not give me the desired result

The extension is working ok. first just the number then the number + and then number - because + comes before - in the ascii tabel.

http://www.asciitable.com/

Simplest question. Why not ordering the list yourself in the right order.

Hi thanks for he reply, this is a simplified version, filters are applied to the data in the tinydb so the data varies depending upon the request so the sample list is just a random possibilty.

Why the sort does not work the way you expect is explained by Peter. When you create your list using a 'number' like 8+, you are not sorting numbers, you are sorting Text and the ascii precedence rules apply.

If you write your own Block code to sort (perhaps using Sajal's tutorial https://imagnity.com/tutorials/app-inventor/list-sorting-on-app-inventor/ ) You probably can modified Sajal's code to treat number - differently in your 'modified' sorting route using Blocks.

The place where you will need to modify his code is probably in the item labeled #10 in the tutorial. Write code to modify precedence when using ( - ) text perhaps to respond as something else?

If you decide on fractional decimal representations of grades, you can sort those decimals and convert back and forth between decimals and grades.

Here is draggable code ...

P.S. No extensions are needed if you use my merge sort blocks:

Hi Derek

A simple way is to use Lists. In this example:

1. There is a fixed List of all Grades in Order (1-, 1, 1+, 2-, 2, 2+ and so on).
2. There is a fixed List of the same size containing no Grades: 'List_GradesSorted'.
3. I have used your example List (named it 'List_GradesUnsorted'.)

How it works

1. Each Grade in the unsorted List is found in the 'List of all Grades in Order', given the index of the ordered grade and added to 'List_GradesSorted'.
2. Once that is done, all the empty Items are removed to make 'List_Final'
3. A ListView is populated with 'List_Final'.

I would like to thank you all for your help and ideas. Peter's comment led me to the idea of replacing the + and minus with text then sorting and replacing back again, so after a couple of hours I have managed it. I initially replaced the - with min + with plus and added norm to the integer, I then used the sortlist extension and then replaced (or deleted) the suffixes , works a treat

2 Likes

Sounds like a great method Derek

1 Like

if I had seen your suggestion before I would have certainly tried it