0

The code that I have works, but the order of the objects isn't correct.

That's what the JSON looks like:

https://i.gyazo.com/102efe767cb0a3d8be8a4a57b6b4560e.png

In the data, in array element 11 the num property has the value 420, the channel number.

What I want to do is iterate over the loop according to the channel numbers (num), not by the order of available_channels as they appear in the JSON. How do I change the loop so that it iterates by channel number?

http://jsfiddle.net/gnpj5csk/231/

var yql_url = 'https://query.yahooapis.com/v1/public/yql';
var url = 'http://***/***.php?username=***&password=***';

$.ajax({
  'url': yql_url,
  //remove this line later
  'data': {
    'q': 'SELECT * FROM json WHERE url="'+url+'"',
    'format': 'json',
    'jsonCompat': 'new',
  },
  'dataType': 'json',
  'success': function(response){
    console.log(response);
    var res = response.query.results.json;
    var keys = Object.keys(res.available_channels);

    for(var i =0;i< keys.length;i++){
      var num = res.available_channels[keys[i]].num;

      $('#channellist').append(num+"<br>");
    }
  }
});

More info:

array 46 - channel number 1:

https://i.gyazo.com/a575219039b8e76c6dc4f7bdc3b99627.png array 26 - channel number 2:

https://i.gyazo.com/3f765e3a73f61d96080cf8fb74de2ae3.png

Order should be..

46 , 26, 47, 27, 48, 24 by the 'num' array order

Right now: 396, 384, 395, 390, 386 (available_channels order)

3
  • Are you saying that you want to sort the numbers before you output them? Your goal isn't clear Commented Jun 20, 2018 at 13:14
  • With PHP it worked fine: ghostbin.com/paste/dst3y | but I'm currently trying with jquery Commented Jun 20, 2018 at 13:16
  • What worked fine? As I mentioned above, the goal you're trying to reach is not clear. In the order you edited in to the question, why would 48 come before 26? Commented Jun 20, 2018 at 13:28

1 Answer 1

1

The sort() function accepts a function as an argument to define alternative sorting ways. To order the available_channels by the channel number try this:

keys.sort(function(a, b) {
    return a.num - b.num;
});

What we are doing here is defining a sorting function that will use the num property from a and b to test which one goes first. Note that if the return is less than 0, a comes first, if the return is greater than 0 b comes first and if it is equal to 0 both come together.

You can find more about the sorting function here

After the sort is done you can simply append with the $.each

$.each(keys, function(i, k) {
    $('#channellist').append(k.num + '<br>');
});
Sign up to request clarification or add additional context in comments.

11 Comments

Thank you very much, Just where should I add the sort function? jsfiddle.net/gnpj5csk/231
You can add it after declaring the keys var, before the for loop on line 17.
What I tried, jsfiddle.net/gnpj5csk/238 What need to fix please?
I've forgot to close the sorting function, updated the answer now. What other errors are you getting?
Also, here var num = res.available_channels[keys[i]].num; where does i comes from?
|

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.