Select to view content in your preferred language

gis.content.search sort_field doesn't work

1181
2
10-24-2019 05:19 PM
LaurinMcKenna
New Contributor

Hello,

I'm using the Python API for arcgis and I'm trying to get information on all our items on arcgis online. However, when I use the gis.content.search() the sort_field option doesn't actually seem to sort the data no matter what I choose. I wrote a test to prove this, and sure enough when choosing to sort by numViews, an item with a lower view count comes before one with a higher view count in the array. Is is not supposed to work this way? Or did I do something wrong? My function looks like this:

def getData(urlusernamepassworditemTypesortTypenumResults😞
    #Send creds to ArcGIS 
    gis = GIS(url=url, username=username, password=password) 
    
    #Check if we want to search by item type or if we wanted all 
    if itemType.lower() == 'all':
        return gis.content.search(query=""sort_field=sortType,
        max_items=numResults)

    return gis.content.search(query=""item_type=itemType, sort_field=sortType,
    max_items=numResults)
0 Kudos
2 Replies
KimberlySaballett
Esri Contributor

Have you tried changing the sort_order in your query? It can be "asc" or "desc".

0 Kudos
Per_Åke_MattiasWallin
Occasional Contributor

Laurin,
I agree with you, the gis.content.search() using a sort_field (sort_order desc or asc) does not work as expected. On the API example page (scroll down to the "Searching for content in ArcGIS Online" section), it doesn’t even order it correctly.

I made a simple workaround that will order your result by the number of views as you can see in the screenshots below.

The standard search doesn't order the results:

After the workaround, the result is sorted by the number of views:

0 Kudos