Retrieving data from a list, based on another sorted list

Hello thunkers.
Today im here for the following issue:
I will simplify things to focus only on result, so lets say ill generate 2 lists, as in image below.
After generating, because were generated in the same time, i will know exactly that the 3rd value in list1 will be “1,3” , and the same 3rd value in list2 will be “c”
But the issue occurs after il want list1 to be sorted.
If all values in list1 are different, all works just fine, using “first occur” in list1 to find correspondent value in list2. But if theres more than 1 value similar in list1, “first occurence” wont do the job.
Ill attach an image to understand my issue better.


Hoping i made myself clear, thank you for providing me an workaround :slight_smile:

1 Like

hi @mimostel I am not that great at list sorting but one thing i can notice is here
image
In the of object Section There is nothing
I dont think thats how the block works.
Correct me if I am wrong

1 Like

ignore that… was only an example… imagine that there is a variable name :slight_smile: (just like in the next blocks

1 Like

You can create a single list of objects. The object has two properties (value1, vlaue2)

Now you can sort or manipulate and the order will be the same.

1 Like

Fortunatly, list1 and list2 are properties of the same object (as you see in block2)
So if i sort list1, list2 will have the same order based on list 1 ? this simplify things alot…
Thank you very much

1 Like

Yes, when they are in an object they will have the same sorting.

1 Like

But how do i retrieve “#” - index of list1`s value to apply in my example for list2 ? (that will be the same, as you said … Maybe ill need a new variable to store as a loop this ?

later edit:
… something like this…

… ill test it as soon as ill get home…
Thank you again

Latwer, after testing.
@muneer , i tested and it doesnt work. Maybe the lists as property of the same object are sorted togheter as i sort one list, but using a variable “contor” to keep index of list1 to retrieve value from list2 as the same index doesnt work. One of these 2 ideeas dont work :frowning: … I guess that my “contor” is the issue, and need something else to retrieve index of list1`s current value

1 Like

It should be structured this way

image

Thank you for your time @muneer , but i still didnt get it :frowning:
Trying to follow your ideea, i made a simple example:


expecting that on start, ill get as result B, D, A, C.
I got instead Error :slight_smile:
Thats because INDEX gets value from list, and not value`s index from list

1 Like

I mean this way

Now, every element in the list has both the number and the corresponding letter.

1 Like

oh, got it now… need to rethink the whole variables for that. thank you.
the problem is that i do not know the lenght of lists, so i need to generate this new object dinamically, but this will do it :slight_smile:

1 Like

To make it clearer, I added a List Viewer and a button in the screen and following is the code in the click event of the button. Every time you click the list will be randomly shuffled but you can see from the list that they relation between the numbers and letters remain unchanged.

1 Like

“shuffle” is the easy thing… but i need it to be sorted by value1 in your examples :frowning: … and dunno how to… and im returning at the beginning of the problem…

1 Like

https://x.thunkable.com/projectPage/60d78e2a5e21e600110b78b4

Check this.

Remove the shuffle block and use app variable myList to see sorted list.

I made it just in case you ask.

1 Like

need to study further to understand all of those… JSON …mmm …
thank you very much for your time…

and finally got the right answer… JSON was the key and with your help i learned something new… thank you

1 Like

That’s nice to know.

I have also added the standard sort algorithm to the project. If you remix now you will see a sort button.

1 Like

Hello thunkers.
@muneer , thank you again for your solution. Works like a charm for 1 digit values, when even if numbers are treated as text, 1 will be smaller as 5 in ascending order, and so on…
But if values are higher, app will say, for example, that 100 is smaler than 5 :)… because as text it will compare 1 to 5 first. (sorting as numbers wont help in your solution…)
Any other ideea to workaround this new issue ? :slight_smile:
Thank you.

1 Like

That is very easy, in the project I shared I included a sorting function. In it I’m comparing “value1”, so if you have it as number it will compare it the correct way or you can include the get property of value1 inside a math function to force it to change to number.

If this is not clear enough, I can change the project to reflect that later when I’m at my computer.

Happy Thunking!

[Edit]

This is how it starts
image

This is after sorting
image

This is the project link

2 Likes

thank you @muneer for taking so much time in this issue…
already applied in my project your first ideea, using JSON :slight_smile:
im not the type that only copy paste someone else`s ideea, so i need to understand 1st what you have done with this new approach, using those functions and so on… :thinking:… so many variables there that cant see it initialized and so on :slight_smile:
thank you again.

1 Like

I’m taking the list as input to the function and take the first element of the list and assume it is the lowest number.
I then going into a while loop (as long as there are entries in the list)
I then go into another loop inside the first loop going through each entry in the list and compare it with my lowest entry.
If the entry in the list is lower than my “lowest entry” then I take the entry in the list making it my new “lowest entry”
At the end of the inner loop my “lowest entry” is removed from the list and added to the result list and start the outer loop again.

If with every iteration of the outer loop you are removing an entry from the list and adding it to the result list then by the end of the loop (when the original list has no more entries) you will have the result list ordered.

That is all in short.

1 Like