Sort divs in a list

1

I have a list of divs, which has a name and a price. I did a function to sort the ASC and DESC divs, store the div (object) and the displayed price in an object.

Then I make the order with a basic function of JS. The problem is that when I try to put it already ordered in the list I see something else.

This is seen at the beginning:

This is the code that is executed when I give raffle, now for tests only serves ASC.

$(document).on('click','.sort',function(){
      sortBy('asc');
    });

    function sortBy(m)
    {
      var result;
      switch (m)
      {
        case 'desc':
          var arr = [];
          $('.propDetails').each(function(){
            var superObj = {};
            var obj = this;
            var value = parseInt($(this).attr('price'));
            superObj.obj = obj;
            superObj.value = value;
            arr.push(superObj);
          });

          arr.sort(function (a, b) {
            if (a.value < b.value) {
              return 1;
            }
            if (a.value > b.value) {
              return -1;
            }
            return 0;
          });

          result = arr;
        break;

        case 'asc':
          var arr = [];
          $('.propDetails').each(function(){
            var superObj = {};
            var obj = this;
            var value = parseInt($(this).attr('price'));
            superObj.obj = obj;
            superObj.value = value;
            arr.push(superObj);
          });

          arr.sort(function (a, b) {
            if (a.value > b.value) {
              return 1;
            }
            if (a.value < b.value) {
              return -1;
            }
            return 0;
          });

          result = arr;
        break;


        default:
        break;
      }

      reBuildList(arr);
    }

Then I put the list back together, right now it does not have many validations if the index exists or not, I'll put it later, but I'm worried that the result can not be shown. This is the code.

function reBuildList(a)
{
  console.log(a);
  var h = "",i=0,ii=1;
  for (i; i < a.length;)
  {
    var obj1 = a[i].obj;
    var obj2 = a[ii].obj;
    h+='<div class="row">' +
      '<div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">' +
        obj1 +
      '</div>' +

      '<div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">' +
        obj2 +
      '</div>' +
    '</div>';

    i = i + 2;
    ii = ii + 2;
  }
  $('#mapList').html(h);
}

The result is this:

This is the impression of the console and the array that was formed

What I am omitting, welcome suggestions.

    
asked by Alberto Siurob 20.12.2017 в 18:26
source

1 answer

0

I already solved it if you need it.

At the time of repainting, I was painting the objects as such. I had to get inside the object and look for the nodes that contained the HTML in string, Here I found them.

function reBuildList(a)
    {
      var h = "",i=0,ii=1;
      h+=lastHeader;
      for (i; i < a.length;)
      {
        var obj1 = a[i].obj.parentNode.innerHTML;
        var obj2 = a[ii].obj.parentNode.innerHTML;
        h+='<div class="row">' +
          '<div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">' +
            obj1 +
          '</div>' +

          '<div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">' +
            obj2 +
          '</div>' +
        '</div>';

        i = i + 2;
        ii = ii + 2;
      }
      $('#mapList').html('');
      $('#mapList').append(h);
    }

Maybe it's not the best way, anyway I appreciate a cleaner way of doing this process.

    
answered by 20.12.2017 в 18:49