I have a custom object called event and it houses the events for a non-profit I am working for. I am trying to find a way to get the events to be sorted alphabetically when the page is pulled up. I am iterating over a list and grabbing each event to be displayed, so does there need to be some sort of recursive step in my code? Any help is greatly appreciated. :)

I am fairly new to Salesforce, so I apologize if this can be answered very easily, but I have yet to find anything online through my research.

Here's my event class:

public class eventClass {
public static List<Event__c> getEvents() {
return [Select Id, Name From Event__c];

Here's my event component:

<aura:component controller="eventClass">
<aura:handler name="init" action="{!c.myAction}" value="{!this}" />
<aura:attribute name="events" type="Event[]" />

    <h1>Choose an Event</h1>
    <aura:iteration items="{!v.events}" var="event">
    <li class="minli"> <ui:outputText value="{!event.Name}" 
    class="eventName"></ui:outputText> <ui:button label="Select" 
    class="eventSelect" press="{!c.eventSelect}"></ui:button> </li>


And lastly, my component controller:

myAction : function(component, event, helper) {
    var action = component.get("c.getEvents");
action.setCallback(this, function(data) {
component.set("v.events", data.getReturnValue());

1 Answer 1


To sort, simply change your return line to this:

return [Select Id, Name From Event__c order by Name];

That should return a sorted list to your Lightning component.

For more info on how to query from the database, you can look here and here.

  • You, sir, just showed exactly how new I am to this. Thank you so much!! Jul 13, 2017 at 19:27
  • We were all new at one point. Glad to help! If this helped you, please upvote/mark as accepted so others can benefit too. Jul 13, 2017 at 19:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .