-1

I tried to retrieve data through a live server and display them through my local web application, the code to fetch the data by the user`s levels.

How I can sort them either by their levels or views entry.heat?

The levels or views entry.heat are values in the server and both are variables, so kindly check the below code in order to sort by entry.heat.

   function(r){
        var max = $('#limit').val();
        if (r.data.video_info.length < max) max = r.data.video_info.length;

        for (index = 0; index < max; index++) {
            var entry = r.data.video_info[index];
            
            var level = parseInt(entry.level);
        

        
            
                            if ((level > $('#min').val()) && (level < $('#max').val())) {
                count++;
                var h = '<div class="entry '+(entry.sex==0?'female':'male')+'"><img src="'+entry.smallcover+'">'+'<span>Heat:<span>'+entry.heat+'</span></h3>';

                $('#main').append(h);

            
        }
    }

            
        if ((current_page < 100) && (count < $('#limit').val() )) {
            current_page++;
            setTimeout(function(){
                doSearch();
            },0);
        }

    }


});
MLavoie
  • 9,671
  • 41
  • 36
  • 56
Amr
  • 1
  • please understand that Java and JavaScript are not the same. Tagging a JavaScript question with Java won't get your question answered any faster. – Stultuske Dec 21 '20 at 13:28

1 Answers1

0

You make it hard to answer your question, since the source code is not formatted correctly. Also you say, you want to sort depending on the value of entry.heat but the code snippet doesn't show any reference to heat.

I'll try to answer though: In order to sort an array in javascript you would like to use your_array.sort(). sort(arg: function) takes a function as argument. In this function you will be presented with two entries of you array. Your function shall tell which of two entries comes first. The function shall return a negative number, if entryB comes before entryA, a positive number if entryA comes before entryB or 0 if both entries are equal in case of the sorting property.

I assume in your case it would be:

your_array.sort((entryA, entryB) => {
  return etryA.heat - entryB.heat;
});

or event shorter:

your_array.sort((a, b) => a.heat - b.heat);

Read on here: JavaScript: Array Sort

Malte Peters
  • 84
  • 1
  • 9