我有正在顯示的數據集,讓我調出定製的.NET代碼和我被困在一個排序問題的應用程序。我的數據集中的一列包含字符串和數字數據,我想按字母順序排序字符串,並用數字排序數字。 我所能做的就是拿出分揀機正在處理的當前值,並返回一些內容。.NET正確排序兼作串數字
如果我的列表是{「-6」,「10」,「5」},我想從這些數字中按字母順序排列字符串。我想出來的是把他們都肯定的,那麼以零填充,這樣的:
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;
}
}
問題:
如果我只是返回號碼,然後它們會按照字母順序排序和10會前5名,我甚至不知道負數會發生什麼。
解決方案?:
我想到的一件事是試圖從雙打(8字節)轉換爲無符號長整型(8字節)。這將消除負數,因爲它們將從0開始。但是10之前的問題仍然存在。對於這一點,也許墊0或東西...
看起來這應該是可能的,但我今天的愚蠢和可以不聰明。
示例數據:
'貓'
'4'
'5.4'
'狗'
'-400'
'土豚'
'12 .23.34.54'
「我一句」
'0'
應該被選到:
'12 .23.34.54'
'土豚'
'貓'
'狗'
'我的一句話'
'-400'
'0'
'4'
'5.4'
你能舉些例子值此列?它們只是附加了雙精度的文本嗎? – BACON 2012-02-10 16:57:46
這裏可以發送比較器嗎? http://msdn.microsoft.com/en-us/library/cfttsh47.aspx – jrsconfitto 2012-02-10 17:07:14
當您只有一個值時,比較函數不起作用。我可以從分類器當前正在評估的值中返回一個字符串。我會在我的描述中更加清楚 – MStodd 2012-02-10 17:31:31