sorting - firebase sort reverse order -
var playersref = firebase.database().ref("team_mapping/"); playersref.orderbychild("score").limittofirst(7).on("child_added", function(data) { }
using query, can sort in ascending order. but, wanted sort in descending order. can body suggest me way this. appreciated.
thanks in advance!!
you can limit last 7 give highest scores since query in ascending order. have reverse last 7 them in descending order.
var playersref = firebase.database().ref("team_mapping/"); var playersdata = []; var pagecursor; playersref.orderbychild("score").limittolast(7).on("child_added", function(data) { playersdata = [data] + playersdata; }
update
here example of how paginate score.
const query = firebase.database().ref("team_mappings").orderbychild('score'); var snapshots = []; function reversedchildren(snapshot) { var children = []; snapshot.foreach(function (child) { children.unshift(child); }); return children; } function addplayertoleaderboard(snapshot) { var key = snapshot.key; var place = snapshots.indexof(snapshot) + 1; var score = snapshot.child('score').val(); $('#leaderboard').append(`<li><b>${key}: ${score}</li>`); } function parsepage(snapshot) { var children = reversedchildren(snapshot); children.foreach(function (child) { players.push(child); addplayertoleaderboard(child); }); pagecursor = children[children.length - 1]; } function reloadplayers() { players = []; pagecursor = null; query.limittolast(5).once('value').then(parsepage); } function loadmoreplayers() { query.endat(pagecursor).limittolast(5).once('value').then(parsepage); } reloadplayers(); $('#reload').on('click', reloadplayers); $('#load_more').on('click', loadmoreplayers);
Comments
Post a Comment