<aside> <img src="/icons/warning_purple.svg" alt="/icons/warning_purple.svg" width="40px" /> Note: this is no longer necessary because sort now supports an expression that you can use to sort relations! See Custom sorting and sorting by related properties solve for the new hotness!

</aside>

In order to sort on a Relation’s property, we must first use map on the relation and return the property we want to sort on. In the following example, we use sort and then reverse to sort the Views in descending order.

Next we use map on the list of Views. For each item we set a temporary views variable and assign it to current. This allows us to use a nested current.

Finally, the lets returns the video with the associated view count.

The final result of this function is the **Videos** ordered by Views descending

prop("Videos")
	/* Find all the views for each video */
  .map(current.prop("Views"))
	/* Sort them... */
	.sort()
	/* ...in descending order */
	.reverse()
	/* Look up the Video page by view */
	.map(
		/* We set a temporary "views" variable here to avoid "current" clash */
		lets(
			views, 
			current, 
			prop("Videos").find(current.prop("Views") == views)
		)
	)

We can take this further by creating a display version:

prop("Videos")
	.map(current.prop("Views"))
	.sort()
	.reverse()
	.map(
		lets(
			views, 
			current, 
			prop("Videos").find(current.prop("Views") == views)
		)
	)
	.map(
		lets(color, index == 0 ? "red" : "",
			current + " (" + 
			current.prop("Views")
				.replace("(\\d{1})(\\d{3})$","$1,$2")
				.replace("(\\d{1})(\\d{3},\\d{3})$","$1,$2")
				.style("b", color) + 
			")"
		)
	)

The last map call creates a list of the videos and displays the view count next to them, highlighting the top performer in red.

SBR Channels

SBR Videos