Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

how to sort optionset items in alphabetical order

Posted on by 2,397

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
    Community Member Microsoft Employee 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
    Community Member Microsoft Employee 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
    sdnd2000 2,397 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
    Community Member Microsoft Employee 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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans