Select to view content in your preferred language

Arcade sorting text help - popup

1705
8
Jump to solution
06-22-2023 09:54 AM
Labels (1)
HarrisonBrown7
New Contributor II

Hello, I am trying to display the closest 3 points (staff locations) to a given feature in a popup. I would like them arranged in order of distance. I have a working expression to show me ALL points within a buffer of the feature, but I just can't figure out how to arrange and truncate them without messing up my resulting string. 

Any tips? TYIA

Screenshot 2023-06-22 115256.png

 

0 Kudos
2 Solutions

Accepted Solutions
PeterMilenkovic
Occasional Contributor

Sorry I miss read your question. I would probably create an empty featureset and populate it in your loop, then you can sort it and get the top 3. 

 

var New_FS = {fields:[        
        {'name':'StaffLocation','type': 'esriFieldTypeString'},
        {'name':'SLDistance','type': 'esriFieldTypeDouble'}],
        'geometryType': '',
        'features':[]
        }
    
for (var s in staff){
    var dist = Distance(s,$feature,'miles')
    var new_f = {'attributes': {'StaffLocation': s['Location'] ,
                                'SLDistance': dist}
                 }
    Push(New_FS.features, new_f)}


 

You would then have to loop through this new fs to construct the string for the popup

View solution in original post

PeterMilenkovic
Occasional Contributor

Sorry, forgot to mention you need to convert the populated dictionary/featureset to read like one.

Try 

var sorted = Top(OrderBy(FeatureSet(Text(New_FS)), 'SLDistance ASC'),3)

 

Cheers

Peter

View solution in original post

8 Replies
Teresa_Blader
Occasional Contributor III

I don't know about truncating the first three, but OrderBy should allow you to at least list them in descending order. I think you would do:

 

var dist = Top(OrderBy(Distance(s, $feature, 'miles'), 'miles DESC'),3)

 

But, I'm not an Arcade expert. 😅 Maybe that goes around Push(Top(Orderby(stafflist....) instead of the Distance function. 

Solved: Re: How to Use Arcade GroupBy with Top and OrderBy... - Esri Community

Teresa Blader
Olmsted County GIS Specialist
0 Kudos
HarrisonBrown7
New Contributor II

Still erroring saying invalid parameter 😕

Maybe because 'miles' isn't a field already? idk

Thanks for the reply though!

0 Kudos
PeterMilenkovic
Occasional Contributor

I would use a top and order by on the intersects

https://developers.arcgis.com/arcade/function-reference/featureset_functions/#top

 

Peter

0 Kudos
HarrisonBrown7
New Contributor II

That didn't work either. My problem is that the feature set I'm wanting to sort only has a name field, and I can't get OrderBy to recognize conditional data from an arcade expression.

Thanks for the suggestion!

0 Kudos
PeterMilenkovic
Occasional Contributor

Sorry I miss read your question. I would probably create an empty featureset and populate it in your loop, then you can sort it and get the top 3. 

 

var New_FS = {fields:[        
        {'name':'StaffLocation','type': 'esriFieldTypeString'},
        {'name':'SLDistance','type': 'esriFieldTypeDouble'}],
        'geometryType': '',
        'features':[]
        }
    
for (var s in staff){
    var dist = Distance(s,$feature,'miles')
    var new_f = {'attributes': {'StaffLocation': s['Location'] ,
                                'SLDistance': dist}
                 }
    Push(New_FS.features, new_f)}


 

You would then have to loop through this new fs to construct the string for the popup

HarrisonBrown7
New Contributor II

This feels closer to what I'm trying to do, but now I can't get the sort to work. Am I doing something wrong?

Thank you

 

 

// create 250mi buffer on selection
var buff = Buffer($feature, 250, 'miles')

// get intersecting staff locations
var staff = Intersects(buff, FeatureSetByName($map, "CS Staff Locations"))

// create empty featureset to hold results
var New_FS = {fields:[        
        {'name':'StaffLocation','type': 'esriFieldTypeString'},
        {'name':'SLDistance','type': 'esriFieldTypeDouble'}],
        'geometryType': '',
        'features':[]
        }
 
for (var s in staff){
    var dist = Distance(s,$feature,'miles')
    var new_f = {'attributes': {'StaffLocation': s['Location'] ,
                                'SLDistance': dist}
                 }
    Push(New_FS.features, new_f)}

var sorted= Top(OrderBy(New_FS, 'SLDistance ASC'),3)

return sorted

 

0 Kudos
PeterMilenkovic
Occasional Contributor

Sorry, forgot to mention you need to convert the populated dictionary/featureset to read like one.

Try 

var sorted = Top(OrderBy(FeatureSet(Text(New_FS)), 'SLDistance ASC'),3)

 

Cheers

Peter

HarrisonBrown7
New Contributor II

This is fantastic, thank you so much!

0 Kudos