0

I just got some data like this

[
    {
        "ID":2,
        "NOTAMRec":"C16-0001",
        "DeliverDate":"310827",
        "BeginDate":"1601010130",
        "ExpireDate":"1606070630",
        "Priority":"GG",
        "ItemA":"LOL",
        "OriginalNotamID":2,
        "SelectedNotamColor":null
    },
    {
        "ID":8,
        "NOTAMRec":"C16-0004",
        "DeliverDate":"230705",
        "BeginDate":"1602231505",
        "ExpireDate":"1606312359 EST",
        "Priority":"GG",
        "ItemA":"LOVEU",
        "OriginalNotamID":8,
        "SelectedNotamColor":null
    },
    {
        "ID":9,
        "NOTAMRec":"C16-0005",
        "DeliverDate":"240703",
        "BeginDate":"1602241502",
        "ExpireDate":"1606312359 EST",
        "Priority":"GG",
        "ItemA":"LOVEU",
        "OriginalNotamID":9,
        "SelectedNotamColor":null
    }
]

and I cast this into a Model

Public Class MyModel 
{
  Public long ID{get;set;}
  public string NOTAMRec{get;set;}
  ......
  public string ItemA{get;set;}
}

And add this into an ObservableCollection.

Problem - I wanted to sort my collection in this order. No matter how many MyModels I add into this collection, Models whose ItemA equals to "LOVEU" will always in the top of this list, and so when I display this list to my user, they will always see the MyModels with LOVEU in the first place.

Thanks!

Nikhil Vartak
  • 5,002
  • 3
  • 26
  • 32
Cagaya Coper
  • 75
  • 1
  • 12

2 Answers2

0

May be it's not the best solution, but this should work

var query1 = MyCollection.Where(w => w.ItemA == "LOVEU");
var query2 = MyCollection.Where(w => w.ItemA != "LOVEU");
MyCollection = new ObservableCollection<MyModel>(query2);
foreach (var item in query1)
{
    MyCollection.Insert(0,item);
}
Amine
  • 1,198
  • 11
  • 19
0

I suppose this should do the trick:

collection.OrderBy(x => x.ItemA == "LOVEU").ThenBy(x => x.ID);

In the second OrderBy you can put any other sorting conditions of course.

Synthetic One
  • 405
  • 2
  • 9