Help us understand the problem. What is going on with this article?

Firebaseから取得したデータをタイムスタンプの降順で並び替えて表示する方法(DESC)

More than 1 year has passed since last update.

FirebaseのRealtime DatabaseにおけるAPIでは現状、昇順ソートでの結果取得しかできないようなので、
降順での並び替え方法についてメモ。
ま、きっといつかは、できるんだろう。って思うけど。

単純に、FirebaseのAPI、.orderByChild(key)で指定キーの昇順で取得したObservable[]の取得データを、
フロント側の.map()内で、array.reverse()で反転して並び替えた。だけ。
です。。。

これ以外にも、最初からデータ上、作成日時のマイナス値をソート用のキーバリューとして保存しておけば、単純なFirebaseでの取得にできるような方法など、他にも、きっと方法はあるんでしょう。

import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import * as moment from 'moment'; 

import { AngularFireDatabase } from 'angularfire2/database';
import { Yell } from '../../model/yell';
@Injectable()
export class YellService {

    constructor(private db: AngularFireDatabase) { }

    getMoments(): Observable<Yell[]> {
        // ★createdAt(作成日時)の昇順でデータ取得し、フロント側で逆順ソート
        return this.db.list<Yell>('/yells', ref => ref.orderByChild('createdAt')).valueChanges().map((yells) => yells.reverse());
    }

    addYell(yell: Yell) {
        yell.createdAt = moment(yell.createdAt).format('YYYY-MM-DD HH:mm:ss')
        this.db.list<Yell>('/yells').push(yell);
    }
}

(Sample)病んどる著者が作成中のYandle
https://yandle-d1d34.firebaseapp.com/moments

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした