4

Given the following values in order :

011124
01112
011123
1905

How could I use Linq to sort this:

List<string> values = new List<string>() { "011124", "01112", "011123", "1905" };

.. so it is effectively this:

List<string> values = new List<string>() { "011124", "011123", "01112", "1905" };

(updated: added '1905' as the previous demo would have sorted easily with an int sort -- sorry for the confusion)

8
  • So you want to store the numbers in descending order by the arthimatic value of the strings? What happens when you have a value of "0000001"? Commented Jul 6, 2015 at 12:20
  • 3
    If you're trying to use the strings to store integers and compare them in an integer fashion, is there any reason you're using strings at all? Life tends to be get a lot easier when you keep data in its "natural" format. Commented Jul 6, 2015 at 12:23
  • Yes, using strings, as some of the numbers require the 0 at the front. Storing as int, removes this. Updating example to add another value into the mix. Commented Jul 6, 2015 at 12:25
  • Thank you everyone for your answers. I wish I could pick everyone for 'correct' solution however since this isn't possible, I have to go with the example that uses Convert.ToInt32() as this returns a 0 on non-convertible numbers where int.Parse() would throw an exception. Both are good, and truly do appreciate everyones input. I have voted everyone up as well :) Commented Jul 6, 2015 at 12:36
  • @SanuelJackson That's not entirely correct. Convert.ToInt32 will internally call int.Parse, which will throw an exception if it is a non-convertible string. Only if the string is null, then it will return 0. Commented Jul 6, 2015 at 12:42

4 Answers 4

14

Try this snippet:

var sortedValues = values
    .OrderByDescending(x => x.Length)
    .ThenByDescending(x => Convert.ToInt32(x));

If you really need use it as a List, then add ToList() at the end.

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

5 Comments

Looks good. could you code format the post again pls. Going to try this.
I am curious about the Convert.ToInt32(x). Is that actually going to be the resulting value (eg : 011123 would end up as 11123) ? I need to keep the 0 in front.
@SanuelJackson It doesn't matter, it's only for the sake of ordering. It will still yield a IOrderedEnumerable<string>.
perfect :). times like this I really wish StackOverflow would let me choose more than one answer as correct. I posted a comment on original thread explaining why I chose yours.
@SanuelJackson See my remarks in the question comments.
4

I'm going to assume you actually care about the value of each integer. If so:

var sortedValues = values.OrderByDescending(x => x.Length)
                         .ThenByDescending(x => int.Parse(x));

This will yield a deferred IOrderedEnumerable<string>. The int.Parse is only for the purpose of secondary ordering. If you need to materialize it, ToArray or ToList will need to be called.

2 Comments

Needs to be string value comparison. The 0 in front matters.
thank you. voted up. see comments in original post for 'answer' selection. really wish I could choose everyone :S .
3

Most of the answers here are written using Lambda Syntax, if you want query syntax try this. Result should be the same.

var sortedValues = from x in values
                   orderby x.Length descending, x descending
                   select x;

5 Comments

orderby x.Length descending, x
@MarioTheSpoon what about it?
Correction. You referring to query vs method syntax. Both are using LINQ.
thank you. voted up. see comments in original post for 'answer' selection. really wish I could choose everyone :S . I do like the variation in syntax. Still kind of new(ish) to Linq coding, so nice to see another example how to use it's query syntax. :)
@FahadJameel first by length, then by content of x (as per OPs question)
2
values.OrderByDescending(x => x.Length).ThenByDescending(x => int.Parse(x));

1 Comment

thank you. voted up. see comments in original post for 'answer' selection. really wish I could choose everyone :S .

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.