javascript - Function execution order -


i have following piece of code:

function() {     $.getjson(         myurl + '/get_data',         function(data) {             function sort_data(first, second) {                 return (first.sort - second.sort);             }             console.log(data);             console.log(data.sort(sort_data));         }         ... snipped brevity 

data array of objects, each object has sort field, integer. default in random order. after executing code above tells me data before , after sort identical. @ least both console.log outputs same (and sorted). however, if skip sorting part , console.log(data.objects) - it's different , unsorted.
seems sort executed first, while console.logs executed after. why that? thanks!

it because console.log() prints reference object, , sort() rearranges data within same object reference.

if want see difference use

console.log(json.stringify(data)); console.log(json.stringify(data.sort(sort_data))); 

json.stringify() creates string representation of json object, not affected further changes made json object.


Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -