List of Dates ordered in a certain way
I have a list of dates (strings in the format yyyyMM
) as follows:
201608
201609
201610
201708
201709
201710
I'd like to reorder them to be as follows:
201608
201708
201609
201709
201610
201710
I thought I could reverse the strings (i.e. 016102
) to order them but obviously with a month >= 10 it causes issues resulting in the list coming out like:
201610
201710
201608
201708
201609
201709
I tried the following:
MyList.OrderBy(n => string.Concat(n.datestring.Reverse())).Select(n => n);
Is there a nice succinct way of doing this (LINQ would be nice)? Thanks for any help.
c# linq sorting
add a comment |
I have a list of dates (strings in the format yyyyMM
) as follows:
201608
201609
201610
201708
201709
201710
I'd like to reorder them to be as follows:
201608
201708
201609
201709
201610
201710
I thought I could reverse the strings (i.e. 016102
) to order them but obviously with a month >= 10 it causes issues resulting in the list coming out like:
201610
201710
201608
201708
201609
201709
I tried the following:
MyList.OrderBy(n => string.Concat(n.datestring.Reverse())).Select(n => n);
Is there a nice succinct way of doing this (LINQ would be nice)? Thanks for any help.
c# linq sorting
You don't want to reverse the strings, you want to slice it into two substrings, year and month and then order by those separately (month, then year). Try something likemyList.OrderBy(s => s.Substring(4)).ThenBy(s => s.Substring(0, 4))
– Brian Rogers
Nov 21 '18 at 20:22
2
So, to be clear, you want them ordered by month and then by year? Or, do you just want them in the order that you say. Consider keep date-ish and time-ish things as either DateTimes or TimeSpans. They are much easier to handle. However, @BrianRogers' solution is probably the way to go (assuming that I've correctly guessed your problem statement)
– Flydog57
Nov 21 '18 at 20:26
Yes that’s the ticket Brian. It’s late. I had a brain fart! Thanks for unsticking me! I’ll give that a go.
– scgough
Nov 21 '18 at 20:28
add a comment |
I have a list of dates (strings in the format yyyyMM
) as follows:
201608
201609
201610
201708
201709
201710
I'd like to reorder them to be as follows:
201608
201708
201609
201709
201610
201710
I thought I could reverse the strings (i.e. 016102
) to order them but obviously with a month >= 10 it causes issues resulting in the list coming out like:
201610
201710
201608
201708
201609
201709
I tried the following:
MyList.OrderBy(n => string.Concat(n.datestring.Reverse())).Select(n => n);
Is there a nice succinct way of doing this (LINQ would be nice)? Thanks for any help.
c# linq sorting
I have a list of dates (strings in the format yyyyMM
) as follows:
201608
201609
201610
201708
201709
201710
I'd like to reorder them to be as follows:
201608
201708
201609
201709
201610
201710
I thought I could reverse the strings (i.e. 016102
) to order them but obviously with a month >= 10 it causes issues resulting in the list coming out like:
201610
201710
201608
201708
201609
201709
I tried the following:
MyList.OrderBy(n => string.Concat(n.datestring.Reverse())).Select(n => n);
Is there a nice succinct way of doing this (LINQ would be nice)? Thanks for any help.
c# linq sorting
c# linq sorting
edited Nov 21 '18 at 20:20
scgough
asked Nov 21 '18 at 20:18


scgoughscgough
3,01121632
3,01121632
You don't want to reverse the strings, you want to slice it into two substrings, year and month and then order by those separately (month, then year). Try something likemyList.OrderBy(s => s.Substring(4)).ThenBy(s => s.Substring(0, 4))
– Brian Rogers
Nov 21 '18 at 20:22
2
So, to be clear, you want them ordered by month and then by year? Or, do you just want them in the order that you say. Consider keep date-ish and time-ish things as either DateTimes or TimeSpans. They are much easier to handle. However, @BrianRogers' solution is probably the way to go (assuming that I've correctly guessed your problem statement)
– Flydog57
Nov 21 '18 at 20:26
Yes that’s the ticket Brian. It’s late. I had a brain fart! Thanks for unsticking me! I’ll give that a go.
– scgough
Nov 21 '18 at 20:28
add a comment |
You don't want to reverse the strings, you want to slice it into two substrings, year and month and then order by those separately (month, then year). Try something likemyList.OrderBy(s => s.Substring(4)).ThenBy(s => s.Substring(0, 4))
– Brian Rogers
Nov 21 '18 at 20:22
2
So, to be clear, you want them ordered by month and then by year? Or, do you just want them in the order that you say. Consider keep date-ish and time-ish things as either DateTimes or TimeSpans. They are much easier to handle. However, @BrianRogers' solution is probably the way to go (assuming that I've correctly guessed your problem statement)
– Flydog57
Nov 21 '18 at 20:26
Yes that’s the ticket Brian. It’s late. I had a brain fart! Thanks for unsticking me! I’ll give that a go.
– scgough
Nov 21 '18 at 20:28
You don't want to reverse the strings, you want to slice it into two substrings, year and month and then order by those separately (month, then year). Try something like
myList.OrderBy(s => s.Substring(4)).ThenBy(s => s.Substring(0, 4))
– Brian Rogers
Nov 21 '18 at 20:22
You don't want to reverse the strings, you want to slice it into two substrings, year and month and then order by those separately (month, then year). Try something like
myList.OrderBy(s => s.Substring(4)).ThenBy(s => s.Substring(0, 4))
– Brian Rogers
Nov 21 '18 at 20:22
2
2
So, to be clear, you want them ordered by month and then by year? Or, do you just want them in the order that you say. Consider keep date-ish and time-ish things as either DateTimes or TimeSpans. They are much easier to handle. However, @BrianRogers' solution is probably the way to go (assuming that I've correctly guessed your problem statement)
– Flydog57
Nov 21 '18 at 20:26
So, to be clear, you want them ordered by month and then by year? Or, do you just want them in the order that you say. Consider keep date-ish and time-ish things as either DateTimes or TimeSpans. They are much easier to handle. However, @BrianRogers' solution is probably the way to go (assuming that I've correctly guessed your problem statement)
– Flydog57
Nov 21 '18 at 20:26
Yes that’s the ticket Brian. It’s late. I had a brain fart! Thanks for unsticking me! I’ll give that a go.
– scgough
Nov 21 '18 at 20:28
Yes that’s the ticket Brian. It’s late. I had a brain fart! Thanks for unsticking me! I’ll give that a go.
– scgough
Nov 21 '18 at 20:28
add a comment |
2 Answers
2
active
oldest
votes
Using LINQ, you order by the two digit month then the year (and month, but that shouldn't matter):
var ans = src.OrderBy(s => s.Substring(4)).ThenBy(s => s);
You beat me to it! This should work fine.
– Elemental Pete
Nov 21 '18 at 20:26
Nice one. Thank you!
– scgough
Nov 21 '18 at 20:29
add a comment |
I'd cheat and order by dates personally, via an Extension Method:
New class:
public static class DateOperations
{
public static List<DateTime> GetDates(this List<string> dateStrings)
{
List<DateTime> asDates = new List<DateTime>();
dateStrings.ForEach(e =>
asDates.Add(DateTime.ParseExact(e, "yyyyMM", CultureInfo.InvariantCulture, DateTimeStyles.None))
);
return asDates;
}
}
And then:
var ordered = myList.GetDates().OrderBy(ord => ord.Month).ThenBy(then => then.Year);
And then when I needed the string again I'd use:
date[_whateverAccessorReally].ToString("yyyyMM");
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419895%2flist-of-dates-ordered-in-a-certain-way%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Using LINQ, you order by the two digit month then the year (and month, but that shouldn't matter):
var ans = src.OrderBy(s => s.Substring(4)).ThenBy(s => s);
You beat me to it! This should work fine.
– Elemental Pete
Nov 21 '18 at 20:26
Nice one. Thank you!
– scgough
Nov 21 '18 at 20:29
add a comment |
Using LINQ, you order by the two digit month then the year (and month, but that shouldn't matter):
var ans = src.OrderBy(s => s.Substring(4)).ThenBy(s => s);
You beat me to it! This should work fine.
– Elemental Pete
Nov 21 '18 at 20:26
Nice one. Thank you!
– scgough
Nov 21 '18 at 20:29
add a comment |
Using LINQ, you order by the two digit month then the year (and month, but that shouldn't matter):
var ans = src.OrderBy(s => s.Substring(4)).ThenBy(s => s);
Using LINQ, you order by the two digit month then the year (and month, but that shouldn't matter):
var ans = src.OrderBy(s => s.Substring(4)).ThenBy(s => s);
answered Nov 21 '18 at 20:24
NetMageNetMage
13.6k12035
13.6k12035
You beat me to it! This should work fine.
– Elemental Pete
Nov 21 '18 at 20:26
Nice one. Thank you!
– scgough
Nov 21 '18 at 20:29
add a comment |
You beat me to it! This should work fine.
– Elemental Pete
Nov 21 '18 at 20:26
Nice one. Thank you!
– scgough
Nov 21 '18 at 20:29
You beat me to it! This should work fine.
– Elemental Pete
Nov 21 '18 at 20:26
You beat me to it! This should work fine.
– Elemental Pete
Nov 21 '18 at 20:26
Nice one. Thank you!
– scgough
Nov 21 '18 at 20:29
Nice one. Thank you!
– scgough
Nov 21 '18 at 20:29
add a comment |
I'd cheat and order by dates personally, via an Extension Method:
New class:
public static class DateOperations
{
public static List<DateTime> GetDates(this List<string> dateStrings)
{
List<DateTime> asDates = new List<DateTime>();
dateStrings.ForEach(e =>
asDates.Add(DateTime.ParseExact(e, "yyyyMM", CultureInfo.InvariantCulture, DateTimeStyles.None))
);
return asDates;
}
}
And then:
var ordered = myList.GetDates().OrderBy(ord => ord.Month).ThenBy(then => then.Year);
And then when I needed the string again I'd use:
date[_whateverAccessorReally].ToString("yyyyMM");
add a comment |
I'd cheat and order by dates personally, via an Extension Method:
New class:
public static class DateOperations
{
public static List<DateTime> GetDates(this List<string> dateStrings)
{
List<DateTime> asDates = new List<DateTime>();
dateStrings.ForEach(e =>
asDates.Add(DateTime.ParseExact(e, "yyyyMM", CultureInfo.InvariantCulture, DateTimeStyles.None))
);
return asDates;
}
}
And then:
var ordered = myList.GetDates().OrderBy(ord => ord.Month).ThenBy(then => then.Year);
And then when I needed the string again I'd use:
date[_whateverAccessorReally].ToString("yyyyMM");
add a comment |
I'd cheat and order by dates personally, via an Extension Method:
New class:
public static class DateOperations
{
public static List<DateTime> GetDates(this List<string> dateStrings)
{
List<DateTime> asDates = new List<DateTime>();
dateStrings.ForEach(e =>
asDates.Add(DateTime.ParseExact(e, "yyyyMM", CultureInfo.InvariantCulture, DateTimeStyles.None))
);
return asDates;
}
}
And then:
var ordered = myList.GetDates().OrderBy(ord => ord.Month).ThenBy(then => then.Year);
And then when I needed the string again I'd use:
date[_whateverAccessorReally].ToString("yyyyMM");
I'd cheat and order by dates personally, via an Extension Method:
New class:
public static class DateOperations
{
public static List<DateTime> GetDates(this List<string> dateStrings)
{
List<DateTime> asDates = new List<DateTime>();
dateStrings.ForEach(e =>
asDates.Add(DateTime.ParseExact(e, "yyyyMM", CultureInfo.InvariantCulture, DateTimeStyles.None))
);
return asDates;
}
}
And then:
var ordered = myList.GetDates().OrderBy(ord => ord.Month).ThenBy(then => then.Year);
And then when I needed the string again I'd use:
date[_whateverAccessorReally].ToString("yyyyMM");
answered Nov 21 '18 at 21:02
Austin T FrenchAustin T French
2,50011429
2,50011429
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419895%2flist-of-dates-ordered-in-a-certain-way%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You don't want to reverse the strings, you want to slice it into two substrings, year and month and then order by those separately (month, then year). Try something like
myList.OrderBy(s => s.Substring(4)).ThenBy(s => s.Substring(0, 4))
– Brian Rogers
Nov 21 '18 at 20:22
2
So, to be clear, you want them ordered by month and then by year? Or, do you just want them in the order that you say. Consider keep date-ish and time-ish things as either DateTimes or TimeSpans. They are much easier to handle. However, @BrianRogers' solution is probably the way to go (assuming that I've correctly guessed your problem statement)
– Flydog57
Nov 21 '18 at 20:26
Yes that’s the ticket Brian. It’s late. I had a brain fart! Thanks for unsticking me! I’ll give that a go.
– scgough
Nov 21 '18 at 20:28