0

I receive an array which I'm not in control of. Sadly, it comes through as a string although all values are always int values.

There is no option to change the array so I have to work with it.

I'd like to sort them but of course, "10" (as a string) comes before "4" (as a string). I understand why.

Without converting the values of the array to int values, is there a way to order the strings as if they were int? I know converting is a better way, but curious as to the option when using string.

In this example, there will not be negative values

My array could be

"10"
"11"
"2"
"4"

but I'd like to show it as

"2"
"4"
"10"
"11"
1
  • 3
    Why can't you convert them to ints? strings.OrderBy(int.Parse) would be the obvious way to do it. Commented Feb 22, 2017 at 20:06

4 Answers 4

2

Yes, you can order them by the length of the string first:

string[] input = /* wherever it comes from */
string[] result = input.OrderBy(s => s.Length).ThenBy(s => s).ToArray();

Note that this is only a theoretical solution for your special interest in "not parsing" and only works if the strings contain no negative values or leading zeros and only the characters '0' - '9' (no special unicode number symbols etc). In productive code you definitely should parse the strings to get proper results, as Servy already correctly pointed out in the comments.

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

7 Comments

According to you "5" is less than "02"
@Servy according to OP the string will be "2" not "02".
The question says no such thing. It says that they're all integers, that's all.
@MyDaftQuestions Are the strings guaranteed to be non negative too?
@MyDaftQuestions The answer isn't correct, and it produces improper results. You should absolutely be parsing the values. That's how you actually get the correct value. That you don't want to do it doesn't change the fact that that is the correct thing for you to do.
|
2

I'm guessing you mean you have to keep them as strings, not that you're literally forbidden to ever parse them at all:

string[] a = GetArray();

a = a.OrderBy(s => int.Parse(s)).ToArray();

4 Comments

I appreciate this is probably the better answer as it's the better way, but as my question was about understanding my option with strings, I've given a plus 1
@MyDaftQuestions Ah, OK. I did say I was just guessing! Cheers.
It's probably what I'll end up doing, but my question wasn't about it. I'd give another +1 if I could
@MyDaftQuestions Oh yeah, not all questions have to be "what's the most respectable boring way to do this in production code". I've written all kinds of weird code just for the sake of it. I wrote a binary adder in Perl once. Talk about useless.
2

You could try something like this:

int maxStringLength = int.MaxValue.ToString().Length;
string[] a = { "1", "11", "20", "2", "10" };
a = a.OrderBy(x => x.PadLeft(maxStringLength, '0')).ToArray();

If you know how big your numbers are going to be, you can use a more reasonable value for maxStringLength.

Comments

1

sort integer in string

  • can sort positive and negatives,
  • can't handle padded with 0..

i dont know ,which one is faster. "int.Parse() or this one."

 List<int> input = new List<int>(){3,2,1, -3,-2,-1, 0};
 var result = sortInteger_inString(input );

 private static List<string> sortInteger_inString(List<string> numAsString)
 {
   //sort -/+ integer in string
   var numAsString_Pos = numAsString.Where(x => !x.TrimStart().StartsWith("-"))
       .OrderBy(s => s.Length).ThenBy(s => s);  
 
   var numAsString_Neg = numAsString.Where(x => x.TrimStart().StartsWith("-"))
       .OrderByDescending(s => s.Length).ThenByDescending(s => s);
   
   return numAsString_Neg.Concat(numAsString_Pos).ToList();
 }

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.