# Sorting a list and at the same time adding together the dupes values

I’m making this app for my school, it consists in taking orders for the food that you want to eat so that we don’t have one person going around the class asking for what they want.
I set up a database and everything, the app is kind of done, I just need to fix this last piece.
In short, I have a list of strings containing the kind of food and how many of each food is ordered so for example:

*PIZZA:
margherita X2
with sausage X2
margherita
with potatoes
with sausage X3

And I want the sorted list to be:
*PIZZA:
Margherita X3
with sausage X5
with potatoes

Dividing the list in two, one containing the name of the food and the second containing the number, this just to handle the data more easily so I have
*PIZZA:
margherita
with sausage
Margherita
With potatoes
With sausage

*QUANTITY:
2
2
1
1
3

Then I check for each value if there is a duplicate, add the value of the duplicates together and at the same time removing the duplicate from the list and add to another list the organized element.
This does work but just for the first element and the others stay blank.
This is the code I used, it has Italian names so maybe it’s hard to understand.

The goal of this post is to try to help me find a better way to handle this or helping me solve my algorithm and making it do what I want it to do.

Here are the translated variables:
nomiPizze= pizza Names (ex: Margherita)
numeroPizze= pizza number (quantity of each name ex: 2)
Tampone= Buffer (used to store temporarily a value)

J/I= while loops counter.

tampNome= buffer of the name (to store temporarily a name)
tampNum= buffer of the number (to store temporarily a number)
vettPizzeOrdinate= vector of organized list (where I store the final values that I will then show)

there is a simple solution, that set a spinner or list for selection, and when a user send order, check if get text = margherita, then add item to list margherita, item 1, else if get item = With potatoes then add item to list , With potatoes , item 1, and then after adding that all at the end you can collect them

This is how the database is set up,
Ordine= order (everything that the user has ordered in one string)

Problem is that the user is able to delete and modify their own order so if I implement that I should do a complete rework of the database and code. So mine was more of an “easy fix” XD since I already have set up that each person has a subtag called “pizza” and there are other sub tags with the name of why they ordered and the value of how many there are.

So should I start from 0 or make the sorting algorithm?

try to configure it first by your own method, and implement a little of mine that add order to a list and compare if list has 2 item same then add them

I’m at school now, I’ll do a “proof of concent” of what I think you mean when I come back home

1 Like

There, i just made the same algorithm in javascript to see if it worked and it does, i think i’ve changed some stuff in this javascript so i will redo it on the app tomorrow and hoping it will work.
First pic is the input, second is the output

And this is the code i wrote: sortingList.txt (3.7 KB)

i really dont understand, the code is the exact same thing (except i used addElement instead of adding the item at the index, i tried both ways but the result is the same) but it doesnt do what it’s supposed to. any help on this?

sortingList.txt (3.7 KB)

well that is a huge mess, i think these are long blocks for that, i can understand them all, let me think something in my way, is your one list is having food names and second list having items counting, then you want to show them like nameX2 , simply use joiner , like when orgnize click set text to ( select name list item 1 ‘join’ select number list item 1 ‘join’ select name list item 2 “join” select number list item 2, etc

Oh ok, I’ve never used those blocks, can I ask you if you could show me an example to understand it better?
I’ll work on it one of those days

This is not what I mean to do, I need to sort the list and remove the duplicates. Look at http://zappavign.it/sortingList.html I made an example there. Your method just joins two lists together, I need to sort them and remove duplicates

ok let me think something… that is a mess,

If you didn’t understand what I need to do, I made this little scheme

Ok i’m so mad at myself right now. all this problem and it was such an easy fix. i forgot to set back to 1 the i2 that i use in the while loop…
This is the working app and .aia if anyone needs it in the futureTestSorting.aia (8.3 KB) TestSorting.apk (4.8 MB)

3 Likes