I tried to solve a problem few days ago and almost solved with the help of SO. Unfortunately stuck into a situation that am unable to sort out. Here's the scenario:
Input:
[7, "aaa", "a", "cccccccccc", 5]
Output:
["a", "aaa", 5, 7, "cccccccccc"]
So you can see depending upon the number of letters in the element, the array should be sorted. In case of number, that should be sorted as well. At one point, the below code worked:
//Dynamic list where we can have string and integers
List<dynamic> aLst = new List<dynamic>();
aLst.Add(7);
aLst.Add("aaa");
aLst.Add("aa");
aLst.Add("cccc");
aLst.Add("a");
aLst.Add(2);
aLst.Add(5);
aLst.Add("cccccc");
int val;
//What I did here, converted all items into string
//Checks if the list contains string
var letters = aLst.Where(i => !int.TryParse(i.ToString(), out val)).OrderBy(i => i.Length).ToList();
//Checks if the list contains number
var numbers = aLst.Where(i => int.TryParse(i.ToString(), out val)).OrderBy(i => int.Parse(i.ToString())).ToList();
//Finally from the number result set, taking the numbers to get their indexes and set them into the list accordingly
foreach (var number in numbers)
{
//Checks number index
var index = int.Parse(number.ToString());
if (letters.Count() >= index)
{
letters.Insert(index, number); //Assign index to numbers
}
else
{
letters.Insert(number);
}
}
foreach(var value in letters)
{
Console.WriteLine(value);
}
The above code gets me the expected result. But when the input is something as follows, it returns different result set that I can't able to set accordingly:
List<dynamic> aLst = new List<dynamic>();
aLst.Add(7);
aLst.Add("aaa");
aLst.Add("aa");
aLst.Add("cccc");
aLst.Add("a");
aLst.Add(2);
aLst.Add(10);
aLst.Add(5);
aLst.Add("cccccc");
aLst.Add("cccccccccccc");
The result I get:
["a", 2, "aa", "aaa", 5, "cccc", 7, "cccccc", "cccccccccccc", 10]
Expected output:
["a", 2, "aa", "aaa", "cccc", 5, "cccccc", 7, 10, "cccccccccccc"]
Anything that I am doing wrong here or missed something? Any idea would be appreciated.
Code snippet: Sort Array Elements