Select to view content in your preferred language

OrderBy An Array Of Features

255
6
Jump to solution
a month ago
Labels (2)
GregReinecke
Occasional Contributor

Hello All,

I have written a bit or arcade (I'm by no means a developer) that currently identifies features within a buffer and puts them in an array. From there I identify the Top 4 features that get pulled.

I'm struggling to 1) sort or OrderBy 'DESC' the resulting four features based on the distance (dist) and 2) limit the output to 3 decimal places.

I have attached the code below and the current result.

 

var intersectsLayer = Intersects(FeatureSetByName($map, "InventoryIntersects"),buff)

var devList =[]

for (var f in intersectsLayer){
  var dName = f.Development_Name
  var dist = DistanceGeodetic(f,$feature,'mile')
  var distNum = Number(dist) 
  var distFull = distNum + " miles to the nearest property " + dName 
   Push(devList, distFull)
}

var topProps = Top(devList, 4)

return topProps

 

Result:

array(4)
0"0 miles to the nearest property El Patrimonio Apartments"
1"1.144445981353735 miles to the nearest property Padre De Vida Apartments"
2"3.2777811013923253 miles to the nearest property Villas at Beaumont"
3"2.470305385835081 miles to the nearest property Uvalde Villas"
0 Kudos
1 Solution

Accepted Solutions
GregReinecke
Occasional Contributor

Thanks Ed and Cody. It looked like the OrderBy would break on the array but the Sort worked fine (see the last couple lines of the final code. Thanks again!!!

var buff = Buffer($feature,4,'mile')
var intersectsLayer = Intersects(FeatureSetByName($map, "InventoryIntersects"),buff)

var devList =[]

for (var f in intersectsLayer){
  var dName = f.Development_Name
  var dist = DistanceGeodetic(f,$feature,'mile')
  var distNum = Round(Number(dist), 2) 
  
  var distFull = distNum + " miles to the nearest property " + dName 
   Push(devList, distFull)
}

Sort(devList)
var topProps = Top(devList, 4)
return Sort(topProps); 

View solution in original post

6 Replies
Ed_
by MVP Regular Contributor
MVP Regular Contributor

What happens if you do:

var topProps = Round(Top(devList, 4), 3)

 

Question | Analyze | Visualize
0 Kudos
GregReinecke
Occasional Contributor

Hi Ed,

I get a number: NaN message. I'm assuming the Round wants a number and devList is an array if I'm understanding of some of this.  I'd like to sort on the distNum value and reduce that to a couple decimal places'

0 Kudos
GregReinecke
Occasional Contributor

I added the Round function to the distNum var and that worked for getting the decimal places where I want them.

var distNum = Round(Number(dist), 2) 

 

Now I just need to sort them. Almost there and thanks so far.

CodyPatterson
Regular Contributor

Hey @GregReinecke 

This OrderBy function should help you out here:

var sortedList = OrderBy(devList, 'dist DESC');

For simplicity I named it sortedList, but you can rename it to devList to keep things together.

Hope that helps!

Cody

Ed_
by MVP Regular Contributor
MVP Regular Contributor

Cool that the "Round" function worked 🙂 now for arranging values in descending order have you tried the "Sort" function? Because the Sort by default arranges values in descending order.

Question | Analyze | Visualize
GregReinecke
Occasional Contributor

Thanks Ed and Cody. It looked like the OrderBy would break on the array but the Sort worked fine (see the last couple lines of the final code. Thanks again!!!

var buff = Buffer($feature,4,'mile')
var intersectsLayer = Intersects(FeatureSetByName($map, "InventoryIntersects"),buff)

var devList =[]

for (var f in intersectsLayer){
  var dName = f.Development_Name
  var dist = DistanceGeodetic(f,$feature,'mile')
  var distNum = Round(Number(dist), 2) 
  
  var distFull = distNum + " miles to the nearest property " + dName 
   Push(devList, distFull)
}

Sort(devList)
var topProps = Top(devList, 4)
return Sort(topProps);