googlearchive / firebase-util

An experimental toolset for Firebase
http://firebase.github.io/firebase-util
MIT License
276 stars 67 forks source link

[Q][Paginate] Is this possible to reverse sort items (limitToLast) ? #75

Closed pandaiolo closed 8 years ago

pandaiolo commented 8 years ago

I have a common usage of the lib for querying items to display in a infinite scrolling list. Item should be sorted by date.

I'd like to display most recent items first. What is the correct way to do this, as I cannot use limitToLast on the Scroll reference ?

Thanks !

pandaiolo commented 8 years ago

Maybe using Paginate instead of Scroll, and using pageCount to query the last page first, and manually sorting the results backward?

kennycrosby commented 8 years ago

+1 would also like to display the most recent items firs and can't seem to orderBy $id which has my timestamp

ginovva320 commented 8 years ago

I think your only option is to use priority. I've used this with success, by setting the priority to a negative timestamp. That way, the newest items are listed first.

katowulf commented 8 years ago

Anything you can do here with priority, you could also do with a field on the record. Priority is not really necessary anymore as you can perform the same ops on fields.

Paginate isn't going to be able to provide descending functionality, since there is nothing in the API to provide this. A field/priority with an inverted value is the right approach.

Sorting by $id is done using orderByKey() or "$key" in the Paginate API. "$id" is an AngularFire concept, not a Firebase one.

kennycrosby commented 8 years ago

@katowulf thanks I didn't realize $id was AngularFire. @ginovva320 I ended up using priority to order my data, was the only way I could get it to work.

juanpabloaj commented 8 years ago

+1

juanpabloaj commented 8 years ago

@pandaiolo from this example

https://gist.github.com/katowulf/54fa9a1e713b61672f53

I use


app.controller('ctrl', function($scope, $pageArray) {
  $scope.pageItems = $pageArray(ref, 'number');

  $scope.pageItems.$loaded().then(function(){
    $scope.pageItems.page.setPage($scope.pageItems.page.pageCount);
  });
});

but I don't like this , because first load the first page and after load, change to last page.

pandaiolo commented 8 years ago

I have written a summary of what techniques I have come across for managing reverse order queries and pagination in Firebase. You can find it here : http://stackoverflow.com/questions/27195851/how-to-retrieve-paginated-children-in-ascending-or-descending-order-in-firebase/33484784#33484784

mubasshir commented 8 years ago

Hi, what is final solution for this? I'm ordering data as per timestamp. What could be better solution?

longsangstan commented 8 years ago

+1!!!! It seems that firebase still doesn't support reverse ordering. The firebase util infinite scroll works really nice but without reverse ordering the use of it becomes very limited.

katowulf commented 8 years ago

Until this feature is part of the SDK, nothing changes here. Add a field to the data with an inverted (negative) value to use descending order. Locking this thread since this isn't an fb-util issue.