0

I have a List:

List<string> allValues = new List<string>();

It may contain values like this:

-3,003-m

2,086

-1, 006-mx

etc.

I sorted it with this:

allValues = allValues.OrderBy(q => q).ToList<string>();
allValues.Reverse();

The problem is, that it somehow does not differ between normal values and negative ones (I'd expect that to work even if they are strings).

So instead of:

3,001 | 3,003-m | 2,086 | 2,145-m | 1,004 | 0,033 | -1,008-m | -2,490

it looks like this:

3,001 | 3,003-m | 2,086 | -2,490 | 2,145-m | -1,008-m | 1,004 | 0,033

If now negative or positive, it seems to treat them as the same, resulting in a somewhat random sorted List.

I am aware that there might be better approaches to this, but I wanted to know if it is possible to resolve this in this specific scenario.

8
  • 3
    You want 3.001, 3.003-m then 2.086 ? what's the logic ? Commented Jan 25, 2016 at 10:21
  • 1
    what is -m or -mx? Commented Jan 25, 2016 at 10:21
  • @Thomas It should sort by the first number, shouldn't it? Commented Jan 25, 2016 at 10:22
  • 3
    String ordering and int/decimal ordering are two different things. Commented Jan 25, 2016 at 10:22
  • 1
    Do you want the result to be greater to smaller? then shouldn't the expected result be: 3,003-m | 3,001 | 2,145-m | 2,086 | 1,004 | 0,033 | -1,008-m | -2,490 ? Commented Jan 25, 2016 at 10:32

2 Answers 2

2

As allValues is a list of strings also the rules for sorting strings apply for sorting. As you need numerical rules of sorting you have to convert the elements to numbers before sorting, e.g. by calling Convert.ToInt32).

So your ordering looks like this:

var result = allValues.OrderBy(q => 
    Convert.ToInt32(q.SubString(0, q.IndexOf(","))))
    .ToList<string>();

Which will perform on only extracting the numeric part and order by it, however it retains the actual strings.

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

3 Comments

This is a comment ... Calling Convert.ToInt32 on -1,008-m will throw an exception.
The problem is the -m / -mx part. I need them in my values, but because of that, I can't convert them to int.
I don't know if someone ever told you this, but you're a genius. Thanks alot!
1

This should do the job:

var result = (from s in allValues
             let val = s.Split(new string[] { "-m" }, StringSplitOptions.RemoveEmptyEntries)[0]
             let val2 = double.Parse(val)
             orderby val2
             select s).Reverse();

The idea is to get the first element divided by "-m", you can simply add the split option with something else ("-mx", "-p", etc) as per necessary.

3 Comments

There is string -mx too in the example, I will suppose there will be more from this.
Yeah, there are multiple possible endings (like -mx or -d), but nonetheless thank you for this idea.
@Ian I didn't say it didn't work. It does indeed work.^^

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.