1

I have some lists of string elements which look like this:

{ Week 2.2012, Week 45.2014, Week 12.2011, ...}
{ Half 1.2013, Half 2.2015, Half 1.2010 ...}
{ Quarter 3.2005, Quarter 4.2010, Quarter 2.2011, ...}

Knowing that these string values cannot be parsed as DateTime (?), how can I sort the lists?

Expected result:

{ Week 12.2011, Week 2.2012, Week 45.2014, ...}
{ Half 1.2010, Half 1.2013, Half 2.2015, ...}
{ Quarter 3.2005, Quarter 4.2010, Quarter 2.2011, ...}
3
  • What would sorted list look like? Commented Dec 10, 2015 at 12:13
  • Something like these: { Week 12.2011, Week 2.2012, Week 45.2014, ...}, { Half 1.2010, Half 1.2013, Half 2.2015, ...}, { Quarter 3.2005, Quarter 4.2010, Quarter 2.2011, ...} Commented Dec 10, 2015 at 12:15
  • 4
    So the sorting is per list, they are never combined? Make that clear(er) in the question. Also add the expected output there. Commented Dec 10, 2015 at 12:19

4 Answers 4

3

Create an implementation of IComparer using your custom logic and pass it to the List.Sort method.

Sign up to request clarification or add additional context in comments.

Comments

0

For this solution i'd like to create a datestring parser which returns date. Next step you could use sorting list of Tuple by datetime.

Comments

0

Actually, it looks like these strings can be parsed as DateTime.

As far as I can understand:

Week 2.2012 is "Second week of 2012" = 15 January 2012

Half 2.2015 is "Six months of 2015" = 1 July 2015 (not quite sure with "half" though - it can have slightly different meaning)

Quarter 3.2005 is "Third quarter of 2005" = 1 October 2005

and so on.

So you can parse these values, convert them to DateTime and sort the lists.

Comments

0

As Andy said you can sort these data by using linq:

var sorted = list.Select(x => x.Split(' ', '.'))
    .OrderBy(x => int.Parse(x[2])).ThenBy(x => int.Parse(x[1]))
    .Select(x => $"{x[0]} {x[1]}.{x[2]}") //or string.Format("{0} {1}.{2}", x[0], x[1], x[2])
    .ToList();

1 Comment

I parse both of them to ensure the format is correct. @Sinatr

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.