Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

how to sort optionset items in alphabetical order

(0) ShareShare
ReportReport
Posted on by 2,508

Hi, I am trying to sort optionset items in alphabetical order through javascript, but it didn't work with "array.sort()", then how should I achieve this?

*This post is locked for comments

  • Verified answer
    Community Member Profile Picture
    on at
    RE: how to sort optionset items in alphabetical order

    Hi sdnd2000,

    try this code.

    function sortOptionSetText()
    {
    var activityType= Xrm.Page.getAttribute("activitytype").getOptions();
    var text = new Array();

    for(var i=35;i<activityType.length;i++)
    {
    if(i <= 148 || i>= 172)
    {
    text.push(activityType[i].text);
    }
    }
    text.sort();

    Xrm.Page.getControl("activitytype").clearOptions();

    for(var j=0;j<text.length;j++)
    {
    for(var k=0;k<activityType.length;k++)
    {
    if(text[j] == activityType[k].text)
    {
    Xrm.Page.getControl("activitytype").addOption(activityType[k], [j])
    }
    }
    }
    }

    If mark as verified if it helps.

  • Verified answer
    Community Member Profile Picture
    on at
    RE: how to sort optionset items in alphabetical order

    Hi sdnd2000,

    try this:

    var optionsarray = [];
    
    var activityOption = Xrm.Page.getAttribute("recoserv_activitytype").getOptions();
    
    for (var i = 0; i < activityOption.length; i++)
    {
        if (i <= 34 || (i >= 148 && i <= 172))
            continue;
    
        var newoption = document.createElement("option");
        newoption.value = activityOption[i].value;
        newoption.text = activityOption[i].text;
    
        optionsarray.push(newoption);
    }
    
    optionsarray.sort(function compare(a, b) {
        var x = a.getText().toLowerCase();
        var y = b.getText().toLowerCase();
        return x < y ? -1 : x > y ? 1 : 0;
    });
    
    activityOption.clearOptions();
    
    for (var j = 0; j < optionsarray; j++)
    {
        activityOption.addOption(optionsarray[j]);
    }

    Please note i didnt test it, maybe it need some adjustments.

    Let me know if works.

    If you found the answer helpful, please mark as Verified 

    Join my network on LinkedIn      Follow me on Twitter 

    Thank You & Best Regards

    Francesco Picchi

    Microsoft Dynamics CRM Consultant, Bologna+Milano, ITALY

    Independent Contractor

    http://www.francescopicchi.com

  • sdnd2000 Profile Picture
    2,508 on at
    RE: how to sort optionset items in alphabetical order

    Hi, Francesco,

    It didn't work for me. Basically, I am trying to remove some option set items on load event, then reorder in alphabetical order, here is my code,

    var activityOption = Xrm.Page.getAttribute("activitytype").getOptions();
    
     for (var i = 0; i<=34; i++)
      { Xrm.Page.getControl("activitytype").removeOption(activityOption[i].value);}
     for (var i = 148; i<=172; i++)
    { Xrm.Page.getControl("activitytype").removeOption(activityOption[i].value);}
    Xrm.Page.getAttribute("recoserv_activitytype").getOptions().sort(function compare (a, b) {
    var x = a.text.toLowerCase();
    var y = b.text.toLowerCase();
    return x < y ? -1 : x > y ? 1 : 0;
    });

  • Suggested answer
    Community Member Profile Picture
    on at
    RE: how to sort optionset items in alphabetical order

    Hi sdnd2000,

    try this:

    var options = Xrm.Page.getAttribute("new_optionset").getOptions();
    options.sort(function (a, b) {
        var x = a.getText().toLowerCase();
        var y = b.getText().toLowerCase();
        return x < y ? -1 : x > y ? 1 : 0;
    });
    

    Please let me know if you solve.

    If you found the answer helpful, please mark as Verified 

    Join my network on LinkedIn      Follow me on Twitter 

    Thank You & Best Regards

    Francesco Picchi

    Microsoft Dynamics CRM Consultant, Bologna+Milano, ITALY

    Independent Contractor

    http://www.francescopicchi.com

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Kudos to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Microsoft Dynamics CRM (Archived)

#1
Mohamed Amine Mahmoudi Profile Picture

Mohamed Amine Mahmoudi 83 Super User 2025 Season 1

#2
Community Member Profile Picture

Community Member 52

#3
Victor Onyebuchi Profile Picture

Victor Onyebuchi 6

Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans