title: Sorting Alertmanager email templates in Go templating
body: <p>I'm trying to customize an Email template from <a href="http://github.com/prometheus/alertmanager" rel="nofollow noreferrer">AlertManager</a>  that uses a Go html template that prints a list of alerts using the following construct :</p>
  
  <pre><code>{{ range .Alerts.Firing }}
  </code></pre>
  
  <p>It gets inserted into the template like this :</p>
  
  <pre><code>func (n *Email) Notify(ctx context.Context, as ...*types.Alert) (bool, error) {
     ...
     data = n.tmpl.Data(receiverName(ctx), groupLabels(ctx), as...)
     ...
  }
  </code></pre>
  
  <p>Alert being defined like this :</p>
  
  <pre><code>type Alert struct {
      Labels LabelSet `json:"labels"`
  
      Annotations LabelSet `json:"annotations"`
  
      StartsAt     time.Time `json:"startsAt,omitempty"`
      EndsAt       time.Time `json:"endsAt,omitempty"`
      GeneratorURL string    `json:"generatorURL"`
  }
  </code></pre>
  
  <p>I would like to do the sorting on the StartsAt field.</p>
  
  <p>I tried using the sort function but it wasn't available in the email template.</p>
  
  <pre><code>{{ range sort .Alerts.Firing }}
  </code></pre>
  
  <p>I'm getting </p>
  
  <pre><code>function \"sort\" not defined
  </code></pre>
  
  <p>Any ideas on how I can get it to sort on StartsAt ?</p>