0

I have these below strings to sort (String Format: 121030-1833 --> YYDDMM-HHMM)

String Example:

121030-1833
120823-2034
120807-2014
120627-2316
120525-1136
111226-1844

I want to sort them from latest to old format.

Which sort method will be the best to use here?

Updated problem statement

dictionary[String,string] "dictLocation" values with me, Want to sort the Dictionary according to its key (Key-YYMMDD-HHMM, latest to oldest)

{[110901-1226, sandyslocation.110901-1226]}
{[120823-2034, andyslocatio.120823-2034]}
{[110915-1720, mylocation.110915-1720]}
{[121030-1833, mylocation.121030-1833]}

I am trying this var latestToOldest = dictLocation.OrderBy(key => key.Key)

It not giving me proper result, Is there anything i am missing ?

1
  • Do you only have the strings or do you have DateTime objects as well? Because if you have the latter you can sort those and then do the string conversion to your required format on the sorted data. Commented Oct 31, 2012 at 12:05

2 Answers 2

4

As it happens, just sorting them in a descending way will work:

var latestToOldest = original.OrderByDescending(x => x);

That's assuming you want to assume all values are within the same century. (If you can possibly change the format, I'd suggest using a 4-digit year for clarity.)

However, I would recommend parsing the values into DateTime values as early as possible. Whenever you can, keep your data in its most "natural" form. Fundamentally these are dates and times, not strings - so convert them into that format early, and keep them in that format as long as you possibly can.

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

1 Comment

I was just about to reference you answer stackoverflow.com/a/9261695/626442...
1
var sortedDateStrings = dateStrings.Sort().Reverse();
// or
var sortedDateStrings = dateStrings.OrderByDescending(x => x);

Comments

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.