Help needed with Objects and Lists

Hi. I’ve got what I think is a simple query. So, I have a local data set which I want to sort. I’m pulling the data from the local data set and putting it into a list of objects (with the sortable field as the first column). My plan is then to sort it by that first column for use elsewhere, but I’m running into problems with accessing the objects in the list and I want to understand where I’m going wrong.

The code block below shows where I’ve got to - but how do I now reference the ‘Example Text’ field of the list to populate a simple list? I’m completely stuck!

Because you’re creating a list of objects, you won’t be able to display that in a Simple List. Simple Lists are for text/numeric items.

But if you want to get a single property from an item in the list, you would get property “Example Text” of item #[integer] of app variable TempList for row id [integer].

If you’re trying to make a list to display, then you would need to make two separate lists, one for “Example Number” and one for “Example Text.” In that case, a Data Viewer List (DVL) is probably preferable.

Thanks. I’ve included an updated set of blocks below. What I’m trying to do is as follows:

  • Take a list if items from a data source and put them into a list of objects (Temp List)

  • Make a new list of objects but in a different order (so the number field is now first) - TempList2 - and reorder them.

  • Create a new list (TempList3) with just a single text field to populate the list viewer.

Everything’s working okay up until the population of TempList3 which isn’t working (the list appears to be empty) and I don’t know what I’m doing wrong. I get your point about a DVL but I’m trying to work it out this way first so I understand it!

Thanks for your help. The project is here: Thunkable

In the future, it’s probably easier to do all of this with Airtable. The Airtable API allows you to use all kinds of filters and sorts in its filterByFormula command. I would remove the space (" ") from your object property names and see if that makes a difference.

If you assign app variable TempList2 to a text input’s text (or a label’s text), what does it display?

And actually, I think I see the problem. app variable TempList is already a list but you put a [gear] list block in front of it which makes it a list of lists. Remove the [gear] list block*.

*I honestly think that the [gear] list block should come with a siren and flashing lights because so many people make this same mistake. You really only need that block when you are creating a new list.

Thanks - getting closer! So, I removed the gear icon and now the list is being populated, but it seems with empty fields. I’ve also removed the spaces from the variable names.

When I set the app variable TempList2 to a label, it shows ‘[object Object],[object Object],[object Object],[object Object],[object Object]’ (for the corrent number of items).

But I’m struggling to retrieve from app variable TempList3 - when I assign the app variable TempList3 to a label, all I get is a row of commas, leading me to think that the list is now being created properly but for some reason not being populated properly.

Thanks for your help with this - really is appreciated!

So this is the difficulty of using a list of objects. When you take an item from a list of objects, you’re getting an object. So to get the values from that object you have to use the get property of object block. But when you are sorting app variable TempList2 there’s nothing to sort by because objects don’t have sortable values… the properties inside the objects do. The for each item j in list loop looks correct to me…

That indicates that the loop is iterating but the get property of object block is returning a blank value.

Would you be able to share a link to the project? It’s hard to troubleshoot this sort of thing without being able to debug and preview it.

Of course - Thunkable

I’ve removed the sort thunk for the time being until I can work out the underlying issue.

It’s much easier to use the for each j in list block with lists but if you want to stick with what you have, you need to make sure you’re getting a unique list item each time through the loop. This is the fix for that:

And here’s the modified project: Thunkable

If you wanted to work with simpler blocks, this does the same thing:

image

Thank you so much. I’ve marked this as solved as you’ve provided a solution, but I do have a follow on question (which I think the answer to is ‘no’!).

Is it possible to sort TempList3 by the first field of the object (ExampleNumber)?

Yes but it’s going to be really complicated programming because of the objects. Again, I’d go with Airtable for something like that. Personally if I was headed that way and didn’t want to use Airtable, I would switch to using a text string instead. For example, if your values were 67 and Tiger, you would create a list item by joining them with a separator such as “#”. So the value of a single list item would be “67#Tiger”. The separator should be a character that will never be part of a possible ExampleText value. Then you can sort the list using the built-in sort block! When you need to get each separate value (ExampleNumber and ExampleText), use the text substring block and other text blocks to find the separator (“#”) and determine which substrings you need for each value.

·····················································································································
Need help? How to Ask Great Questions :sparkles: Debugging A Project :sparkles: API JSON Tutorial

Want to hire a Certified Thunkable Expert? Elevate your app with Tatiang on Fiverr

1 Like

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