tags:

views:

181

answers:

5
string strArr="5,3,8,1,9,2,0,6,4,7";

I would like to rearrange the order of the numbers so the result will look like the following:

string result ="0,1,2,3,4,5,6,7,8,9";

Any idea?

+21  A: 

Split, sort and join:

string[] nums = strArr.Split(',');
Array.Sort(nums);
string result = String.Join(",", nums);

Or:

string result =
  String.Join(",",
    strArr.Split(',')
    .OrderBy(s => s)
    .ToArray()
  );

If you have a string with larger numbers that need to be sorted numerically, you can't sort them as strings, as for example "2" > "1000". You would convert each substring to a number, sort, and then convert them back:

string result =
  String.Join(",",
    strArr
      .Split(',')
      .Select(s => Int32.Parse(s))
      .OrderBy(n => n)
      .Select(n => n.ToString())
      .ToArray()
  );

Or, as mastoj suggested, parse the strings in the sorting:

string result =
  String.Join(",",
    strArr
      .Split(',')
      .OrderBy(s => Int32.Parse(s))
      .ToArray()
  );
Guffa
first non-linq solution wouldn't work starting from 2character strings, e.g. "10" will be less than "2". +1 for linq though.
Grozz
your last linq query is to long. It should be enough with String.Join(",", strArr.Split(',').OrderBy(n => Int32.Parse(n)).ToArray());
mastoj
I think this will solve the problem: string result = String.Join(",", strArr.Split(',').Select(p => Convert.ToInt32(p)).OrderBy(s => s).Select(p => p.ToString()).ToArray());
Homam
It'll solve the problem but was unnecessary long, I eliminated two of the function calls in the linq-chain.
mastoj
@mastoj: Yes, that is shorter, and probably better for this example. To find out if it's more efficient it would have to be profiled. Doing the parsing in the sorting means that each string will be parsed several times instead of only once. However, another effect is that a string like "007" is left unchanged instead of being parsed into 7 and then converted to "7".
Guffa
@Guff: You're completely regarding performance, the generated query needs to be profiled to see if it is more efficient. But I think your're query is faster due to multiple parsings as you sort of say in your comment :). Everything come down to williams problem. Is there a performance issue? How does he want to handle strings like "007"? Etc.
mastoj
i want explanation for the last one..
william
I am not sure about what (s => .....) means.. and I don't think I will have values like 007.
william
@william: `s => ...` is a lambda expression (google c# lambda). If you are not useing framework 3.5 you can use the first solution with `Array.Sort`.
Guffa
yea.. thanks. I m using framework 2.0
william
+2  A: 

By splitting and joining:

string strArr = "5,3,8,1,9,2,0,6,4,7";
string[] sArr = strArr.Split(',');
Array.Sort(sArr);
string result = string.Join(",", sArr);
Ruel
A: 

You can create a string array with the string.split(char[]) method.

With this array you can call the Array.Sort(T) method, that will sort the items into ascending numerical order (for your example).

With this sorted array you can call the String.Join(string, object[]) to pull it together into a string again.

Matt Ellen
+3  A: 

Shorter version of one of the versions in Guffa's answer:

var res = String.Join(",", str.Split(',').OrderBy(y => int.Parse(y)).ToArray());
mastoj
A: 
string arr = "5,3,8,1,9,2,0,6,4,7";
string result = arr.Split(',').OrderBy(str => Int32.Parse(str)).Aggregate((current, next) => current + "," + next);
888