Answer the question
In order to leave comments, you need to log in
Why doesn't it output in order from the database?
I created a code for extracting records from the database (I use the parse.com service),
but it displays the records in a scatter, and you need the newest ones at the top
xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "https://api.parse.com/1/classes/Blog", true);
xmlhttp.setRequestHeader('Content-Type', 'application/json');
xmlhttp.setRequestHeader('X-Parse-Application-Id', 'GKid48BAvytRnKn8Y8CFKiKQ8jIJSYwpUsr1vID1');
xmlhttp.setRequestHeader('X-Parse-REST-API-Key', '6bnW8hVQNvuLQYiDiXvMHwIvV0inEne93gLzOC0A');
xmlhttp.onload = function () {
if (xmlhttp.status === 200) {
var userInfo = JSON.parse(this.responseText);
console.log(this.responseText);
var arr = userInfo.results.reverse();
var text = "";
for (var i = 0; i < arr.length; i++) {
text += '<div class="blog-post"><div class="blog-header">'+arr[i].title + '</div><br><img width="560px;" src="' + arr[i].img + '"><br><div class="blog-text">' + arr[i].text + '</div><div class="blog-share"><a href="http://gamer-by-life.com/share/?title='+arr[i].title + '&text='+arr[i].text + '&img='+arr[i].img + '&time='+arr[i].createdAt + '"><img src="share.png"></a></div><br></div><br>';
}
document.getElementById('blogs').innerHTML=text;
}
};
xmlhttp.send();
Answer the question
In order to leave comments, you need to log in
Sorting must be done before reverse:
function sortDate(a,b){
var aDate = new Date(a.createdAt);
var bDate = new Date(b.createdAt);
if(aDate == bDate)
return 0;
if(aDate < bDate)
return -1;
if(aDate > bDate)
return 1;
};
xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "https://api.parse.com/1/classes/Blog", true);
xmlhttp.setRequestHeader('Content-Type', 'application/json');
xmlhttp.setRequestHeader('X-Parse-Application-Id', 'GKid48BAvytRnKn8Y8CFKiKQ8jIJSYwpUsr1vID1');
xmlhttp.setRequestHeader('X-Parse-REST-API-Key', '6bnW8hVQNvuLQYiDiXvMHwIvV0inEne93gLzOC0A');
xmlhttp.onload = function () {
if (xmlhttp.status === 200) {
var userInfo = JSON.parse(this.responseText);
console.log(this.responseText);
debugger;
userInfo.results = userInfo.results.sort( sortDate );
var arr = userInfo.results.reverse();
var text = "";
for (var i = 0; i < arr.length; i++) {
text += '<div class="blog-post"><div class="blog-header">'+arr[i].title + '</div><br><img width="560px;" src="' + arr[i].img + '"><br><div class="blog-text">' + arr[i].text + '</div><div class="blog-share"><a href="http://gamer-by-life.com/share/?title='+arr[i].title + '&text='+arr[i].text + '&img='+arr[i].img + '&time='+arr[i].createdAt + '"><img src="share.png"></a></div><br></div><br>\n';
}
console.log(text);
}
};
xmlhttp.send();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question