I have an app which is displaying a data set which allows me to call out to custom .NET code and am stuck on a sorting problem. One column in my data set contains both strings and numeric data, and I want to sort the strings alphabetically and the numbers numerically. All I can do is take the current value the sorter is working with, and return something.
If my list is {"-6", "10", "5"}, I want to produce strings from those numbers that would sort them alphabetically. What I've come up with is making them all positive, then padding with zeros, like this:
public object Evaluate(object currentValue)
{
//add 'a' to beginning of non-numbers, 'b' to beginning of numbers so that numbers come second
string sortOrder = "";
if(!currentValue.IsNumber)
sortOrder = "a" + currentValue;
else
{
sortOrder = "b"
double number = Double.Parse(currentValue);
//add Double.MaxValue to our number so that we 'hopefully' get rid of negative numbers, but don't go past Double.MaxValue
number += (Double.MaxValue / 2)
//pad with zeros so that 5 comes before 10 alphabetically:
//"0000000005"
//"0000000010"
string paddedNumberString = padWithZeros(number.ToString())
//"b0000000005"
//"b0000000010"
sortOrder += paddedNumberString;
}
}
Problem:
If I simply return the number, then they get sorted alphabetically and 10 will come before 5, and I don't even know what will happen with negative numbers.
Solution?:
One hack I thought of was trying to convert from doubles (8 bytes) to unsigned longs (8 bytes). That would get rid of negative numbers since they would be starting at 0. The problem of 10 coming before 5 still remains though. For that, perhaps pad with 0s or something...
It seems like this should be possible, but I have the dumb today and can't smart.
example data:
'cat'
'4'
'5.4'
'dog'
'-400'
'aardvark'
'12.23.34.54'
'i am a sentence'
'0'
that should be sorted to:
'12.23.34.54'
'aardvark'
'cat'
'dog'
'i am a sentence'
'-400'
'0'
'4'
'5.4'