Search code examples
javascripthtmlalphabetical

How to sort alphabetically part of the HTML page (images and text)


I need to order each of the items in the gallery by alphabetical order. Each item would have an image and title.

Screenshot of the working code

The code for the gallery:

<section id="content">
<div class="main">
   <div class="container_24">
       <div class="padding-1">
          <div class="wrapper">
              <article class="grid_24">
                  <h4 class="prev-indent-bot">Piante da interno</h4>
                           <div class="wrapper">
                               <div class="col-3 spacing-3">
                                    <div class="border">
                                        <a class="lightbox-image" href="images/Interno/Phalaenopsis_big.jpg" data-gal="prettyPhoto[gallery2]"><img src="images/Interno/Phalaenopsis_small.jpg" width="224" height="208" alt=""></a>
                                    </div>
                                    <div class="bg-title">
                                        <div class="box-padding1">
                                            <h5>Phalaenopsis</h5>
                                        </div>
                                    </div>
                               </div>
                               <div class="col-3 spacing-3">
                                    <div class="border">
                                        <a class="lightbox-image" href="images/Interno/Capelvenere_big.jpg" data-gal="prettyPhoto[gallery2]"><img src="images/Interno/Capelvenere_small.jpg" width="224" height="208" alt=""></a>
                                    </div>
                                    <div class="bg-title">
                                        <div class="box-padding1">
                                            <h5>Capelvenere</h5>
                                        </div>
                                    </div>
                               </div>
                               <div class="col-3 spacing-3">
                                    <div class="border">
                                        <a class="lightbox-image" href="images/Interno/Kalanchoe_big.jpg" data-gal="prettyPhoto[gallery2]"><img src="images/Interno/Kalanchoe_small.jpg" width="224" height="208" alt=""></a>
                                    </div>
                                    <div class="bg-title">
                                        <div class="box-padding1">
                                            <h5>Kalanchoe</h5>
                                        </div>
                                    </div>
                               </div>
                               <div class="col-3">
                                    <div class="border">
                                        <a class="lightbox-image" href="images/Interno/Nertera_big.jpg" data-gal="prettyPhoto[gallery2]"><img src="images/Interno/Nertera_small.jpg" width="224" height="208" alt=""></a>
                                    </div>
                                    <div class="bg-title">
                                        <div class="box-padding1">
                                            <h5>Nertera</h5>
                                        </div>
                                    </div>
                               </div>
                           </div>
                        </article>
                    </div>
                </div>
            </div>
        </div>
    </section>

Is there a simple solution using javascript or html?

P.S. sorry for my english.


Solution

  • Here's a version that I've tested on the site you've linked:

    var articles = $('article.grid_24');
    articles.each((art_idx, el)=>{
        var art = $(el);
        var boxes = art.find('.wrapper .col-3'); //Get the boxes that have the image and name
    
        boxes.removeClass('spacing-3'); //Trim out this, since we don't know if any given box will have this after resorting
        boxes.detach(); //Remove the entries without losing their JS attributes
        boxes.sort((a,b)=>{return ($(b).find('h5').text() < $(a).find('h5').text())?1:-1;}); //Sort by the name in the h5 title
    
        var wrappers = art.find('.wrapper'); //Get the rows
        var wrapper_idx = -1; //At the very first element, this will be incremented to index 0
        boxes.each((idx, box)=>{ //For each content box
            if(idx % 4 === 0) wrapper_idx++; //For each fourth content box, move to the next row
            if((idx+1) % 4) $(box).addClass('spacing-3'); //If it's not the fourth box, give it back this class
            $(box).appendTo($(wrappers[wrapper_idx])); //Add the content box into the current row
        });
    });
    

    Edit: I have changed this to keep the different elements sorted only between their respective article parents.

    This can be inserted as a Javascript block after all of the images have loaded. I do agree with Josh Miller in that, ideally, this sorting would actually be done before rendering the content, but the above code should work if the content is already displayed.