2

My Service

get ActivityList():any {
    return this.activityList.snapshotChanges().map(changes => {
        return changes.map(c => ({ 
            key: c.payload.key, ...c.payload.val();
        }));
    });
}

My Component

getActivity() {
    this.activityService.ActivityList.subscribe(activity => {
        this.activity = activity;
    });
}

HTML

<div *ngFor="let item of activity">
    {{item.created|date:'medium'}}
</div>

Question

This is how I am currently getting my activity from Firebase RealTime Database. where and how is the best way to modify this to sort by the created value.

2 Answers 2

1

change service to

get ActivityList():any {
    return this.activityList.snapshotChanges().map(changes => {
        return changes.map(c => ({ key: c.payload.key, ...c.payload.val() })).sort((a, b) => b.created - a.created );
    });
}
Sign up to request clarification or add additional context in comments.

Comments

1

Sort it after subscribe.

getActivity() {
    this.activityService.ActivityList.subscribe(activity => {
        this.activity = activity.sort((a, b) => a.create - b.create );;
    });
}

5 Comments

working on solving this now changes.map(...).do is not a function
this is not working. Im trying different variations of this.
just adding my 2 cents, add that sort that sachila wrote in a pipe to have that functionality in other places without adding more code.
ERROR TypeError: list.sort is not a function .... also trying the pipe
grrr, do operator should work. Ok i guess u have to sort it inide the subscribe, check the update

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.