Sorting entities for use in choice lists

1. What is the issue? Please be detailed.
I uploaded a list of locations to an entity list, pre sorted in the order I wished the choices to display.
The list in Central shows row 1 at the bottom as entry 1, and the last row at the top, so the file was read from top to bottom.

When I use this entity list in a Select, the order is reversed, displaying them in the order they were added to the entity list, most recent first.

I need the ability to sort choices, be it A-Z / elevation / priority / incomplete first / smallest to largest (for proper rendering of polygons on map to still allow selection without blocking) or any other method to aid in usability. Currently with CSV choice lists I generate the list in the preferred order and then create the file.

Knowing how they are processed means reverse sorting the list before uploading will work, but this doesn't work for items created at different times or where the preferred order will change as the survey progresses.

I can't see how the choices can be ordered based on any value in the entity list, there's nothing in the docs or on the forum that I could turn up on sorting entities or choices, only that they are now sorted by creation order:

4 Likes

Being able to sort a choice list populated by entities would also be something that I'd be interested in for the linguistic example explained here: Retrieving Dynamic Media from Entity - #9 by Tyler_Depke

1 Like

You can see some prior discussion of sort functions at Support for XForm Sort Expression - #3 by LN

We're currently working on supporting offline Entities and as we design how Entities are stored on-device for offline creation and updates, we are considering an eventual sorting requirement.

1 Like

I just created a entity list of our 470 projects to be ordered alphabetically in collect. So am I also interested by an ordering capability :wink:

3 Likes

On a note as I was browsing the doc and I have not seen this info on the forum, you can already add a sortby property for entities, it will order your entities when using search (the column just needs to NOT contain empty values). You can reorder manually if you have a small amount of entities. Obviously if you want something more dynamic (i.e. ordering alphabetically while managing a high number of entities), my guess is that you would have to edit this property in all entities using the API, after new entities have been created.

Thanks a lot @Thalie !
I did use it with external csv for very long long species list queried by search() function.
I didn't try with entities.

I found reference to this in the XLSForm docs after you posted it, but sortby isn't mentioned in the ODK docs so I hadn't seen it before!

I don't use search for my choice filtering and prefer to use instance. It appears that a search can have at most two levels of filtering only, with the first allowing contains/startswith/endswith/matches and the second only being an exact match, which isn't as flexible as instance lookups that allow other types of filtering and more than two constraints.

I guess sortby is specific to search currently so including that column wouldn't be useful (yet), but perhaps some of my filters are simple enough to accomplish with search so I should start adding it to my entity lists. As sortby is numeric I would have to manually edit or use the API to correctly sort lists that are A-Z

1 Like

Hi @ahblake,

Oh, that’s a good point - Apologies I ran many tests over the weekend and mistakenly thought sortby was working with search (which I regularly use with entities) because I had seen it working with search() while doing some testing, hence the ambiguity. I admit this makes it much less useful for entities since you the search() syntax is not really "best practice" and is not compatible with certain ODK features, such as choice_filter.

So to be clear, the first syntax search(...) used with select_one / select_multiple leads to a sorted list (but is not best practice), while the second syntax search used with select_one_from_file / select_multiple_from_file does not

Survey tab
image

Choices tab
image

Entity list

Screenshot for select_one and search() (sorted by sortby)

Screenshot for select_one_from_file and search (not sorted by sortby)

1 Like