1

I need to get the elements that come during search in alphabetical order.

This is my code:

<input type="text" id="refNumber" class="floatLabel" asp-for="ReferenceNumber" required>

Here's my code in jQuery:

  //autocomplete
            $("#refNumber").autocomplete({
                source: function (request, response) {
                    $.ajax({
                        url: "@Url.Action("GetProspectsName", "PlacementRequest")",
                       /* url: "/PlacementRequest/GetAllProspects",*/
                        dataType: "json",
                        data: {
                            searchText: request.term
                        },
                        success: function (data) {
                            response($.map(data, function (item) {
                                return {
                                    label: item.fullName + '-(' + item.referenceNumber + ')',
                                    value: item.referenceNumber,
                                    data: item
                                };
                            }));
                        }
                    });
                },
                minLength: 1,
                select: function (event, ui) {
                    prospectDetails(ui.item.data.id);
                },
                open: function () {
                    $(this).removeClass("ui-corner-all").addClass("ui-corner-top");
                },
                close: function () {
                    $(this).removeClass("ui-corner-top").addClass("ui-corner-all");
                }
            });

Currently my autocomplete appears in the following way

This is how my autocomplete appears at the moment.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 3
    why cannot your API GetProspectsName return the data in alphabetical order instead? – Anand Sowmithiran Jan 27 '22 at 05:32
  • 2
    You can achieve what you want on the server side as well as on the client side, in this case I would recommend the server side though as mentioned above. – Rahul Sharma Jan 27 '22 at 05:38
  • 1
    If you have some limitation and want to sort in client side, you can see how to do it using `sort` function in this [SO answer](https://stackoverflow.com/questions/4222690/sorting-a-json-object-in-javascript). – Anand Sowmithiran Jan 27 '22 at 05:43

0 Answers0