-1

I need to order the items that are defined in a list called preferences.

List<String> preferences = new List<String> { "first","second","third" };
IEnumerable<mylist> orderedData = mylist.OrderBy(item => preferences.IndexOf(item));

ordereddata output should be

first
second
third... //Remaining property values comes here

please give the suggestion.

Ajay2707
  • 5,690
  • 6
  • 40
  • 58
  • 1
    Possible duplicate of [Multiple "order by" in LINQ](http://stackoverflow.com/questions/298725/multiple-order-by-in-linq) – psubsee2003 May 25 '16 at 06:31

2 Answers2

0

Look for an index if available in preference list else use int.MaxValue to keep it at the end.

List<String> preferences = new List<String> { "first","second","third" };
IEnumerable<MyList> orderedData = mylist.OrderBy(item => 
                                         {
                                              var index = preferences.IndexOf(item);   // use property if item is an object.
                                              return index>=0 ? index: int.MaxValue;
                                         });

Check this example

Hari Prasad
  • 16,716
  • 4
  • 21
  • 35
0
        List<string> lst = new List<string>() { "b", "d", "f" };
        List<string> main = new List<string>() { "a", "g", "e" };
        main = main.OrderByDescending(O => O.ToString()).ToList();
        lst.Reverse();
        main.AddRange(lst);
        main.Reverse();

        foreach (var item in main)
        {
            Console.WriteLine(item);
        }
King
  • 179
  • 5