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.

2
  • 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(...). Commented Jul 31, 2011 at 15:54
  • Related: stackoverflow.com/questions/1134976/… Commented Jul 31, 2011 at 16:39

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.