Dhiraj kadam
Dhiraj kadam

Reputation: 377

Firebase Realtime data key order checking

Is it possible to check if a key exists before or after a given reference key?

Here is my posts node:

-LFdM9Dfy7t-xiK-pO5h
-LFdPpn7wsRM3Y5TUPiN
-LFdQ0x1T6OF_FiCeIWk
-LFg41ZcFHgrI0VSY83v
-LFiTmj1OceTiAYqCflV

suppose my reference key is '-LFdPpn7wsRM3Y5TUPiN' I want to check key '-LFg41ZcFHgrI0VSY83v' exists before or after my reference key. Kinda got confused how to do it.

Upvotes: 0

Views: 44

Answers (2)

nabil london
nabil london

Reputation: 521

Reference keys are just strings, so you can compare them lexicographically like this:

firstKey.compareTo(secondKey)

this method returns a positive, negative or zero int based on the order, and because the first 48 bits of the keys are timestamps, the keys are ordered chronologically.

Upvotes: 2

Niklas Fehde
Niklas Fehde

Reputation: 57

Just loop though the reference keys and remember the index of '-LFdPpn7wsRM3Y5TUPiN' and '-LFg41ZcFHgrI0VSY83v'. Then check which number is bigger.

EDIT:

Try the following:

if( yourReferenceKeyArray.indexOf( first key ) > yourReferenceKeyArray.indexOf( second key ) ) 

Upvotes: 0

Related Questions