0

We are using this URI:

/_api/web/lists/getByTitle('MyListName')/fields/getbytitle('MyColumn')

to get the choice values from the column. I need to pull them into an array alphabetically, but the $orderby.asc is not working.

Is there a way to get this collection back in alpha ascending order?

1 Answer 1

1

As per my understanding there is no direct way to get the choices from choice field in alphabetical order using SharePoint REST API (correct me If I am wrong).

But you can sort the choices after you got the result from REST API using JavaScript. Use sort() method like below in success function:

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort();

Reference: JavaScript Array sort() Method.

This is the code I have used before to achieve the same:

$.ajax({
    url: <siteUrl> + "/_api/web/lists/GetByTitle('List Name')/fields?$filter=EntityPropertyName eq 'Choice Field Internal Name'",
    type: "GET",
    headers: {
        "accept": "application/json;odata=verbose",
    },
    success: function (data) {
        var choicesArray = [];
        if(data && data.d.results) {
            choicesArray = data.d.results[0].Choices.results.sort();
        }
    },
    error: function (error) {
        alert(JSON.stringify(error));
    }
});

You can get the internal name of your choice column by following this article: How to find the Internal name of columns in SharePoint Online?

Update:

You can also sort the array in Microsoft Flow/Power automate. Please check below links for possible solution.

  1. Sort an array in Power Automate in 3 easy steps
  2. How to implement Sort with Microsoft Flow in 3 actions within a loop
4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.