-1

[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": "mehmet@bilsoft.com",
      "subeAdi": "Merkez"
    },
    {
      "id": 7,
      "grup": "ALICI",
      "kullaniciAdi": "mehmet@bilsoft.com",
      "subeAdi": "Merkez"
    },
    {
      "id": 8,
      "grup": "SATICI",
      "kullaniciAdi": "mehmet@bilsoft.com",
      "subeAdi": "Merkez"
    },
    {
      "id": 9,
      "grup": "SATIŞ",
      "kullaniciAdi": "mehmet@bilsoft.com",
      "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
}
BySuspect
  • 13
  • 5

1 Answers1

0

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

Maddy
  • 774
  • 5
  • 14
  • this is not solved my problem – BySuspect Jul 28 '22 at 07:55
  • `OrderBy` returns a new `IEnumerable` so you'd want `var sortedList = _pickerlistItemsSource.OrderBy(var=> var.ID)` or something to that effect – lee-m Jul 28 '22 at 07:58
  • `_pickerlistItemsSource = new ObservableCollection(_pickerlistItemsSource.OrderBy(i => i.ID));` this solved my problem thx – BySuspect Jul 28 '22 at 08:10
  • @BySuspect: Getting the idea of sorting your collection should be good enough to take it further based of your needs. – Maddy Jul 28 '22 at 08:27