0

I currently have a firebase project that parses data from the web.

All the data is coming down however I'm wanting to reverse order the results.

I achieve this by doing the following code

public class ExampleItemModel{

    public string name;
    public string score;
    public string level;
    public int iconIdx1, iconIdx2, iconIdx3;
}

Then in my fetch data method :-

    public  void GetDataFromFireBase(string reference, Action<ExampleItemModel[]> onDone){


         FirebaseDatabase.DefaultInstance
          .GetReference(reference).OrderByChild("score")
          .GetValueAsync().ContinueWith(task => {
            if (task.IsFaulted) {
              // Handle the error...
              print("Error: " + task.Exception);
            }
            else if (task.IsCompleted) {
              DataSnapshot snapshot = task.Result;
              // Do something with snapshot...

                var items = snapshot.Value as Dictionary<string, object>;
                int count = items.Count;
                var results = new ExampleItemModel[count];
                int i = 0;
                foreach(var item in items){
                    results[i] = new ExampleItemModel();

                    //print("items =  " + item);
                       var values = item.Value as Dictionary<string, object>;

                            if (reference == "scores"){
                            print("values count =" + values.Count);

                                foreach(var value in values){

                                    if(value.Key == "name"){ results[i].name = "" + value.Value;}
                                    if(value.Key == "score"){ print("Score = " + value.Value); results[i].score = "" + value.Value;}
                                    if(value.Key == "level"){ results[i].level = "" + value.Value;}

                                }
                            i++;
                            }
            onDone(results);

}

          });

}

Ive tried the following : -

in firebase rules I've set

  ".indexOn":"score",
"score": {
  ".indexOn": ".value"
}

I've also tried to add the following line of code

            results = results.OrderByDescending( x => x.score);
            onDone(results);

but I get the following error

severity: 'Error' message: 'Cannot implicitly convert type 'System.Linq.IOrderedEnumerable' to 'ExampleItemModel[]' [Assembly-CSharp]' at: '137,14' source: ''

Would anybody please be able to help

thanks

AL.
  • 36,815
  • 10
  • 142
  • 281
Tom
  • 2,358
  • 1
  • 15
  • 30
  • This is usually done by indexing on a new field that has inverted values. See this answer: https://stackoverflow.com/a/34158197/4816918 – Jeff May 30 '17 at 20:18

1 Answers1

0

Thanks Jeff for your response

On the back of this I managed to order the results by doing the following

List <ExampleItemModel> orderedArray = results.OrderByDescending(x => x.score).ToList();

Hope this will help somebody else with this issue in the future

Tom
  • 2,358
  • 1
  • 15
  • 30