Home > Enterprise >  Cant Sort ObservableCollection List by any Item
Cant Sort ObservableCollection List by any Item

Time:07-28

[duplicate] solution on here


i want sort my ObservableCollection list but i cant find how.

I saw this but idk how can use

I want sort;

ObservableCollection < CariHesaplarPickerItems > _pickerlistItemsSource = new ObservableCollection < CariHesaplarPickerItems > ();

by ID.

My main code:

public List < string > pickerListItems = new List < string > {...};

ObservableCollection < CariHesaplarPickerItems > _pickerlistItemsSource = new ObservableCollection < CariHesaplarPickerItems > ();

private async Task GetAllData() 
{
  ...

  var resCariGrup = await client.ExecuteAsync(request, Method.Post);
  var dataCariGrup = JsonConvert.DeserializeObject < RootCariGrup > (resCariGrup.Content);

  _pickerlistItemsSource.Clear();
  pickerListItems.Clear();
  pickerListItems.Add("Hepsi");

  for (int i = 0; i < dataCariGrup.data.Count(); i  ) 
  {
    ...
  }
  pickerCariListe.ItemsSource = pickerListItems;

  ...
}

List items:

public class CariHesaplarPickerItems
{
    public int ID { get; set; }
    public string grupAd { get; set; }
    public string kullaniciAd { get; set; }
    public string subeAd { get; set; }
}

I want sort this items

{
  "data": [
    {
      "id": 10,
      "grup": "kasa alaçak",
      "kullaniciAdi": "demo",
      "subeAdi": "Merkez"
    },
    {
      "id": 4,
      "grup": "MÜŞTERİ",
      "kullaniciAdi": "demo",
      "subeAdi": "Merkez"
    },
    {
      "id": 5,
      "grup": "TOPTANCI",
      "kullaniciAdi": "[email protected]",
      "subeAdi": "Merkez"
    },
    {
      "id": 7,
      "grup": "ALICI",
      "kullaniciAdi": "[email protected]",
      "subeAdi": "Merkez"
    },
    {
      "id": 8,
      "grup": "SATICI",
      "kullaniciAdi": "[email protected]",
      "subeAdi": "Merkez"
    },
    {
      "id": 9,
      "grup": "SATIŞ",
      "kullaniciAdi": "[email protected]",
      "subeAdi": "Merkez"
    },
    {
      "id": 11,
      "grup": "asansör bakımları",
      "kullaniciAdi": "demo",
      "subeAdi": "Merkez"
    },
    {
      "id": 12,
      "grup": "genel giderler",
      "kullaniciAdi": "demo",
      "subeAdi": "Merkez"
    },
    {
      "id": 13,
      "grup": "TAŞERON",
      "kullaniciAdi": "demo",
      "subeAdi": "Merkez"
    }
  ],
  "totalCount": 9,
  "success": true,
  "message": null,
  "code": null
}

CodePudding user response:

You can do something like below to order your collection based on ID

_pickerlistItemsSource.OrderBy(var=> var.ID)

This will return an ordered list of objects

  • Related