Sort Picklist Values in Lightning Component | Salesforce Tutorials
In this article, you will learn to sort the values in Picklist in Lightning Component. You just have to add an additional line to your controller or helper of your Lightning Component.
Don't forget to check out: Top Salesforce Lightning Components to Improve Functionality
Controller
fetchPicklistValues: function(component, fieldName, elementId, plType) { var action = component.get("c.getselectOptions"); action.setParams({ "objObject": component.get("v.objInfo"), "fld": fieldName }); var opts = []; action.setCallback(this, function(response) { if (response.getState() == "SUCCESS") { var allValues = response.getReturnValue(); if (allValues != undefined && allValues.length > 0) { opts.push({ class: "optionClass", label: "--- None ---", value: "" }); } allValues.sort(function(a, b){return a - b}); for (var i = 0; i < allValues.length; i++) { opts.push({ class: "optionClass", label: allValues[i], value: allValues[i] }); } component.find(elementId).set("v.options", opts); } else{ var errors = response.getError(); console.log('###Error'+errors[0].message); } }); $A.enqueueAction(action); }
Output
Check out another amazing blog by Kapil here: Formula to Get Time Zone Based on Area Code in Salesforce
Also, check out some amazing videos by Kapil HERE.