web
You’re offline. This is a read only version of the page.
close
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,510

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

I have the same question (0)
  • Suggested answer
    Community Member Profile Picture
    on at

    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

  • sdnd2000 Profile Picture
    2,510 on at

    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;
    });

  • Verified answer
    Community Member Profile Picture
    on at

    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

  • Verified answer
    Community Member Profile Picture
    on at

    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.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans