-1

My question is, how to take from this List

Periods = new List<TimePeriod>
                  {
                    new TimePeriod
                      {
                        Id = "Today",
                        Start = DateTime.Now.Date,
                        // date without seconds
                        End = DateTime.Now.Date.AddDays(1).AddTicks(-1) // last before next day
                      },

                    new TimePeriod
                      {
                        Id = "Yesterday",
                        Start = DateTime.Now.Date.AddDays(-1),
                        End = DateTime.Now.Date.AddTicks(-1),
                      },

                    new TimePeriod
                      {
                        Id = "CurrentWeek",
                        Start = DateTime.Now.FirstDayOfWeek(),
                        End = DateTime.Now.LastDayOfWeek().AddDays(1).AddTicks(-1)
                      },

                    new TimePeriod
                      {
                        Id = "LastWeek",
                        Start = DateTime.Now.AddDays(-7).FirstDayOfWeek(),
                        End = DateTime.Now.AddDays(-7).LastDayOfWeek().AddDays(1).AddTicks(-1)
                      },

only Ids(Today,Yesterday,CurrentWeek,LastWeek) and put them in some other List, so i can sort them in my function

public void FillDefaultPeriods(int offsetInMinutes,List<string> ordering)

with ordering parameter( for example i will put in parameter later "Yesterday","Today","LastWeek","CurrentWeek"). The order will be not the same like in Periods List

user2171512
  • 531
  • 1
  • 11
  • 28
  • 1
    Are you sure you've posted relevant sorting method code? Your `FillDefaultPeriods` doesn't attempt to sort anything. – Andrey Korneyev Apr 10 '15 at 09:07
  • How do you want sort? Based on Start Time or End Time? – SelvaS Apr 10 '15 at 09:08
  • no no, in that method i want to put Id values from Periods in some List and than sort that list. Argument ordering will give me the order of the new List – user2171512 Apr 10 '15 at 09:09
  • So you want to order based on the Arguments? – SelvaS Apr 10 '15 at 09:10
  • possible duplicate of [How to Sort a List by a property in the object](http://stackoverflow.com/questions/3309188/how-to-sort-a-listt-by-a-property-in-the-object) – mainvoid Apr 10 '15 at 09:11
  • Start time and End time are no relevant, i want sorting based on Periods Id's("Today","Yesterday","Last Month"...). Now i have sorting like Today,Yesterday,.. , but i want to have method that will have parameter for sorting – user2171512 Apr 10 '15 at 09:12

2 Answers2

1

Well, it is not quite clear how your posted FillDefaultPeriods relevant to your declared goal "put those Id ("Today","Yesterday".. in some other List) and then sort them", but this goal can easily be achieved by:

var list = Periods.Select(p => p.Id).ToList();
list.Sort();

Update

This will sort list using defult string comparer. If you need some custom comparing option - you can use override of List.Sort method takes Comparison<T> as argument and write your own comparison rules

like this:

var list = Periods.Select(p => p.Id).ToList();
list.Sort(MyCustomComparison);

....

private static int MyCustomComparison(string x, string y)
{
   ... your comparing logic here.
}

In MyCustomComparison method you have to write your comparing logic. This method should return negative value if x is "less" than y by you logic, zero if they are equals and positive value if x "greater" than y.

Andrey Korneyev
  • 26,353
  • 15
  • 70
  • 71
  • Actually the OP wants to be sorted the list based on the parameter. – SelvaS Apr 10 '15 at 09:15
  • Ok but how to make the sort order by my taste ? With your code i will list all the Id and put in var list. But how how to make sorting in that list with parameter in the function. For example if i want Yesterday to be my first item, Today my second.. or maybe i will want next time to have today first, Yesterday second – user2171512 Apr 10 '15 at 09:18
  • Yes i want to have sorting bu parameter – user2171512 Apr 10 '15 at 09:18
0

If you want to work with the periods themselves in a particular order and not only have their ids sorted, you could go with this:

var orderedPeriods = Periods.OrderBy(p => p.Id);

This will use default string comparison on ids. Of course, you can provide another key deriving function if you want something more sophisticated.

michaelb
  • 451
  • 2
  • 11