-1

I currently have a problem, I have a submenu with the following code:

load_sub_menu_disciplines: function() {
    $.ajax({
headers: {
    "Accept": "application/json; charset=utf-8",
    "Content-Type": "application/json; charset=utf-8"
},
type: 'GET',
url: 'api/catalog_system/pub/specification/fieldvalue/22',
success: function(data) {
    $.each(data, function(i, ele) {
        if (ele.IsActive) {
            $('#disciplinas .box-link .nav').append($('<li/>').append($('<a/>').attr('href', '/' + ele.Value + '?map=specificationFilter_22&O=OrderByReleaseDateDESC').text(ele.Value)))
        }
    });
    $('#disciplinas a:empty').remove();
},
error: function(error) {
    console.log(error);
}

});

And I need you to be sorted alphabetically, but I have no idea how to do it. I understand that I should do it with a Sorting function, but I have no idea how to unify the code. Im noob in js -.-

Vane
  • 11
  • 1

1 Answers1

-1

The simplest way to handle this is to sort your data before appending it to the DOM.

success: function(data) {
    data.sort(function(a, b) {
        if (a.Value == b.Value) { return 0; }
        if (a.Value > b.Value)  { return 1; }
        return -1;
    });

    $.each(data, function(i, ele) {
        if (ele.IsActive) {
            $('#disciplinas .box-link .nav')
                .append($('<li/>')
                .append($('<a/>')
                .attr('href', '/' + ele.Value + '?map=specificationFilter_22&O=OrderByReleaseDateDESC')
                .text(ele.Value)))
        }
    });
    $('#disciplinas a:empty').remove();
},
Jonathan Bender
  • 1,911
  • 4
  • 22
  • 39