How do I get the List from firebase sorted?

Bruce Krueger 331 Reputation points
2023-01-16T01:25:36.04+00:00

The items display but I want them to be in ClubId order but can't get it to sort. What do I need to fix?

Model/Club.cs

 public class Club
        { 
            public int ClubId { get; set; }
            public string DateTime { get; set; }
            public string Heading { get; set; }
            public string Location { get; set; }
            public string Image { get; set; }
            public string Height { get; set; }
            public string WebLink { get; set; }
        }

Helper/FirebaseHelper.cs

public async Task<List<Club>> GetAllClub()
        {
            return (await firebase
              .Child("Club")
              .OrderByKey()
              .OnceAsync<Club>()).Select(item => new Club
              {
                  Heading = item.Object.Heading,
                  DateTime = item.Object.DateTime,
                  Location = item.Object.Location,
                  Image = item.Object.Image,
                  Height = item.Object.Height,
                  WebLink = item.Object.WebLink,
                  ClubId = item.Object.ClubId
              }).ToList();
        }

ClubPage.xaml.cs

protected async override void OnAppearing()
        {
            base.OnAppearing();
            var allClub = await firebaseHelper.GetAllClub();
            lstClub.ItemsSource = allClub;
        }
Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,297 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,319 questions
{count} votes

Accepted answer
  1. Bas H 141 Reputation points
    2023-01-16T07:25:03.7733333+00:00

    Try this. OrderByDescending with BalId

    
     
    public async Task<List<Club>> GetAllClub()
            {
                return (await firebase
                  .Child("Club")
                  .OnceAsync<Club>()).Select(item => new Club
                  {
                      Heading = item.Object.Heading,
                      DateTime = item.Object.DateTime,
                      Location = item.Object.Location,
                      Image = item.Object.Image,
                      Height = item.Object.Height,
                      WebLink = item.Object.WebLink,
                      ClubId = item.Object.ClubId
                 }).OrderByDescending(x => x.ClubId).ToList();
            }
    
    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 69,146 Reputation points Microsoft Vendor
    2023-01-31T02:37:16.1366667+00:00

    Hello,

    How can I get the Maximum ClubId?

    You have ordered by descending ClubId with GetAllClub method. you can get the maximum ClubId at the last record in List<Club>. You can refer to the following code.

    List<Club> clubs   = await  GetAllClub();
    var MaxClubId =  clubs.Last<Club>().ClubId;
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments