0

I want to sort a list of custom objects by object property numerical values.

For example, I have custom object MyObject with properties:

MyObject.A
MyObject.B
MyObject.C

Where A is a string containing number values, B and C are Strings containing text.

I want to sort the list containing these objects by the A property from smallest number to largest.

I tried using this:

MyList = MyList.OrderBy(Function(i) i.A).ToList()

But this sorts the list as if the numbers were strings.
For example, I get the result: 1,10,11,2,3,4.
What I actually wanted was this: 1,2,3,4,10,11

How can I sort the list by numerical values?

3
  • Have you tried codeproject.com/Articles/11016/Numeric-String-Sort-in-C Commented Oct 14, 2016 at 13:07
  • On a side note, if property A is number, why are you using a string data type? Why not integer or double? Commented Oct 14, 2016 at 13:44
  • Because the values in those objects are written by a different sub. They are extracted from long strings with delimiters (input data) to a string array which is then written to these objects. Commented Oct 14, 2016 at 13:51

1 Answer 1

1

Then this is not a double-property but a string which displays doubles. Either always convert this string to double which isn't very efficient, or store them as double and only convert them to strings where you need to display them (recommended).

However, here is the Parse approach:

MyList = MyList.OrderBy(Function(i) Double.Parse(i.A)).ToList()

If it contains dots and you use commas(as commented) use:

MyList = MyList.OrderBy(Function(i) Double.Parse(i.A, CultureInfo.InvariantCulture)).ToList()
Sign up to request clarification or add additional context in comments.

6 Comments

My bad. Yes, A item was actually a string containing a double. Anyway, tried parsing and got an error: "Input string was not in a correct format." However, when I try to change A to double in the original definition, I get an error: i.imgur.com/nznTR2P.png
@EdgarsMiškins: if it's a string you can't make it a double by casting it. You have to parse it. What i wanted to recommend is: dont store string in the first place. Where is it coming from?
You get the exception if your input string is not in the correct format or you are using the wrong cultureinfo for it(f.e. one that uses dot as decimal separator and your string contains commas). There are plenty of questions and answers on stackoverflow that cover this issue. To parse 1,2 to double if your pc uses a different culture you could use: double.Parse("1,2", new CultureInfo("de-DE"))
Thank you for replying. The latter error (about conversion to double not being valid) seems to be coming from another sub that is generating this list. Sub is in an external dll library so it's not very convenient to debug that. So I guess, it would be faster to go with original list and find a way to convert this string to double for this sort operation. As opposed to trying to change the external sub. The string contains a dot "." which is supposed to be the decimal separator. I'll try some more ideas with parsing. Gonna post updates if anything works.
@EdgarsMiškins: if it contains dots and you use commas use: double.Parse("123.45", CultureInfo.InvariantCulture)
|

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.