I have a list of strings something like:-
"008","a", "007","b", "c","009"
Need OutPut:-
"a", "b", "c", "007", "008", "009" What i tried so far
string[] things= new string[] { "aaul", "bob", "lauren", "007", "008", "009"};
foreach (var thing in things.AsQueryable().OrderByDescending(x => x, new SemiNumericComparer()))
{
Console.WriteLine(thing);
}
public class SemiNumericComparer: IComparer<string>
{
public int Compare(string s1, string s2)
{
if (IsNumeric(s1) && IsNumeric(s2))
{
if (Convert.ToInt32(s1) > Convert.ToInt32(s2)) return 0;
if (Convert.ToInt32(s1) < Convert.ToInt32(s2)) return 1;
if (Convert.ToInt32(s1) == Convert.ToInt32(s2)) return -1;
}
if (IsNumeric(s1) && !IsNumeric(s2)){
return -1;
}
if (!IsNumeric(s1) && IsNumeric(s2)){
return 1;
};
return string.Compare(s1, s2, true);
}
public static bool IsNumeric(object value)
{
try {
int i = Convert.ToInt32(value.ToString());
return true;
}
catch (FormatException) {
return false;
}
}
}
Here is problem fiddle using above solution
I am able to get output like
a,b,c,009,008,007 Or 007,008,009,c,b,a but need a,b,c,007,008,009 Any Idea?