3

I would like to ask: I have a array with predefined order.

var order=new Array(); 
order[0]="Tesco";       
order[1]="Interspar";
order[2]="Penny Market";

etc..

And i need to write out items in predefined order.

I can write out item in order as is, but i cannot writeout in predefined order.

$.each(mapdata, function(index, value) {

    //IF IS CHAIN IN ARRAY obchody, exit LOOP , ELSE ADD CHAIN TO LIST  
    if (jQuery.inArray(value.retezecnazev, obchody) === -1) {

        obchody.push(value.retezecnazev);

        countOfProducts++;

        //CHANGE DATE FORMAT 
        var choppedDate = value.platido.split('-');

        var newDate = choppedDate[2] + "." + choppedDate[1] + "." + choppedDate[0];

        $("ul").append("<li data-icon=\"false\"><a data-ajax=\"false\"  href=\"show_flyer.html?id=" +
                value.id +
                "&countOfPages=" +
                value.strany +
                "&nameOfChain=" +
                value.retezecnazev +
                "  \"><img  style=\"margin-top:10px;\" src=\" " + value.img + " \"/><h3 style=\"font-size:1.5em;\">" +
                value.retezecnazev +
                "</h3><div class=\"pageCount\" style=\"font-size:0.7em;\">" +
                value.strany +
                " Stran</div><div style=\"font-size:0.7em;\" class=\"pageCount\">Platnost do: " +
                newDate +
                " </div></a> </li>");
    }
});

Is anybody who can help me and know how it solve?

Thanks very much for any advice.

kpozin
  • 25,691
  • 19
  • 57
  • 76
redrom
  • 11,502
  • 31
  • 157
  • 264
  • Is the `mapdata` argument an Array or a plain JavaScript Object (a hashmap)? If you're iterating over an Object's properties, the order is not guaranteed. You'll probably want to pass the `order` array into `$.each(...)`. – kpozin Jul 31 '11 at 15:54
  • Related: http://stackoverflow.com/questions/1134976/how-may-i-sort-a-list-alphabetically-using-jquery – Phill Pafford Jul 31 '11 at 16:39

1 Answers1

1

Do you want to sort the array the very first time it's written and then leave it as is, or do you want to allow the user to sort the array after it's already on the page? If you don't care about sorting the array after the page has loaded, then apply a sort immediately before you loop over mapData:

http://www.w3schools.com/jsref/jsref_sort.asp

commadelimited
  • 5,656
  • 6
  • 41
  • 77