Since the messages are displayed in reverse order, you must reverse the order of the "pages" (use endAt instead of startAt ) and sort the messages back on each page.
See also this answer.
Example
Setup:
$scope.data = []; var n = Math.ceil(($(window).height() - 50) / (350)) + 1; var firstElementsLoaded = false; var lastScore = MAX_SCORE, lastKey
Function for scroll event listener:
$scope.getDataset = function() { fb.orderByChild('score') .endAt(lastScore, lastKey) //endAt is inclusive, take one more for the n-th .limitToLast(n + firstElementsLoaded) .once('value', loadPosts) function loadPosts(snapshot) { var posts = snapshot.val() function compare(a, b) { if (posts[a].score != posts[b].score) { return b.score - a.score } if (a < b) return 1 if (a > b) return -1 return 0 } //skip the post included by endAt var ordered = Object.keys(posts).sort(compare).slice(firstElementsLoaded) lastKey = ordered[ordered.length-1] lastScore = posts[lastKey].score ordered.forEach(function(key) { $scope.data.push(posts[key]) }) $scope.$apply(); firstElementsLoaded = true; } }
For a more elegant way, you can try to keep the ratings back or use an extra value.
fb.orderByChild('reversedScore')
source share