{% set ns = namespace(ea=[]) -%}
{%- for e in states.light -%}
{%- set ns.ea = ns.ea + [({'entity_id':e.entity_id,'area':area_name(e.entity_id)})] -%}
{%- endfor -%}
{%- set sorted_list = (ns.ea) | sort(attribute='area') -%}
sorted_list = {{sorted_list}}
A one-liner that might give some ideas: this gives a list containing a list of entities within each area, where the areas are sorted alphabetically but not shown, for all areas where you have a light :
Dictionary with areas (where you have a light) as keys and a list of all entities therein as values:
{% set al = states.light|map(attribute='entity_id')|map('area_name')|reject('none')|unique|list|sort %}
{% set ns = namespace(ad={}) %}
{% for a in al %}{% set ns.ad = dict(ns.ad, **{a:area_entities(a)}) %}{% endfor %}
{{ ns.ad }}
Waiting for one of the gurus to swoop in and solve it in about 16 characters…