Javascript, list in alphabetical order is not working

Asked

Viewed 56 times

1

This code is to create a list of blogger posts in alphabetical order. The post title and image is displayed.
I changed this code to change the position of the image and the title and create class, the image appeared after the title, everything is in a single line, text and image.
But after the change the list stopped being in alphabetical order, I could not identify the error, I’m still learning javascript.

That’s the part I changed:

          a1E.href = url;
          a1E.textContent = title;
          a1E.className += "aclass";
          postImage.src = imageThumb;
          postImage.className += "imclass"; 
          liE.appendChild(postImage);
          liE.appendChild(a1E);


Before changing:

              a1E.href = url;
              a1E.textContent = title;
              postImage.src = imageThumb;
              liE.appendChild(a1E);
              liE.appendChild(postImage);


complete code:

  var startIndex = 1;
  var maxResults = 150;
  var allResults = [];
  function sendQuery12()
  {
    var scpt = document.createElement("script");
    scpt.src = "/feeds/posts/summary/-/Series?alt=json&callback=processPostList13&start-index=" + startIndex + "&max-results=" + maxResults;
    document.body.appendChild(scpt);
  }
  function printArrayResults(root)
  { 
    //Sort Alphebetically
    allResults.sort(function(a, b){
      var a_string = a.children[0].textContent ;
      var b_string = b.children[0].textContent ;
      if(a_string < b_string) return -1;
      if(a_string > b_string) return 1;
      return 0;
    })
    var elmt = document.getElementById("postList13");
    for (index = 0; index < allResults.length; index++) {
      elmt.appendChild(allResults[index]);
    }
  }
  function processPostList13(root)
  {   
    var elmt = document.getElementById("postList13");
    if (!elmt)
      return;
    var feed = root.feed;
    if (feed.entry.length > 0)
    {
      for (var i = 0; i < feed.entry.length; i++)
      {
        var entry = feed.entry[i];
        var title = entry.title.$t;
        var date = entry.published.$t;

        if( entry.media$thumbnail != undefined ){
          var imageThumb = entry.media$thumbnail.url ;
        } else {
          var imageThumb = 'https://i.imgur.com/PqPqZQN.jpg' ;
        }

        for (var j = 0; j < entry.link.length; j++)
        {
          if (entry.link[j].rel == "alternate")
          {
            var url = entry.link[j].href;
            if (url && url.length > 0 && title && title.length > 0)
            {
              var liE = document.createElement("li");
              var a1E = document.createElement("a");
              var postImage = document.createElement("img");

              a1E.href = url;
              a1E.textContent = title;
              a1E.className += "aclass";
              postImage.src = imageThumb;
              postImage.className += "imclass";

              liE.appendChild(postImage);
              liE.appendChild(a1E);



              //elmt.appendChild(liE);
              allResults.push(liE);

            }
            break;
          }
        }
      }
      if (feed.entry.length >= maxResults)
      {
        startIndex += maxResults;
        sendQuery12();
      } else {
        printArrayResults();
      }
    }
  }
  sendQuery12();

HTML: <div><ul id="postList13"></ul></div>

  • As <li> that you are creating are within a <ul>?

  • Yes it is, I put the html wrong, I’ll fix it

  • Would not be postList13 the id, as it is in the code?

  • Yes, I put it wrong again, now it’s all right

1 answer

1


As I replied in this post, you can reorder the <li> in alphabetical order using .map(), .sort() and .filter(). The code below will do the job by replacing the list HTML with a new sorted list. Place it after the for where you are creating the <li>:

var lis = elmt.querySelectorAll("li"); // seleciona as LIs
var ordenada = ''; // crio uma variável vazia que será usada para construir uma nova lista ordenada

[].map.call(lis, function(a){ // mapeio as LIs retornando o HTML
   return a.textContent+a.outerHTML; // coloco o texto no início da LI
}).sort().filter(function(a){ // ordeno com .sort()
   ordenada += a.match(/<li>.*<\/li>/); // pego apenas a LI e concateno
});

elmt.innerHTML = ordenada; // substituo o conteúdo da UL pelos novos LIs ordenados
  • I changed here, but I think I did something wrong, because it didn’t work, can you send me the full code, please? if possible keep the comments

  • I was really doing wrong, I ended up putting in for

  • Did not work, link code working on blogger: http://testenextdark.blogspot.com/p/teste.html

  • Thank you very much.

  • I thought it worked, but I think it was just a problem with the page loading, when I reloaded the page, it remained out of order, but thanks for helping

  • yes I know how to use the console?

  • I figured out what’s causing the problem, liE.appendChild(postImage); is coming before liE.appendChild(a1E); and is being the order according to the image, but I need the image to come before the title, it is possible to change this without changing the order of the list?

  • Coincidentally the link of the images made them in order, I changed the images, can check the code and see that it is the same.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.