函數如何將字符串內容傳遞給下面的「比較器」函數?C#:傳遞字符串以低於
public static void Sort(XmlNodeList nodes, Comparison<XmlElement> comparer)
{
// The nodes.Count == 0 will break the nodes[0].ParentNode,
// the nodes.Count == 1 is pure optimization :-)
if (nodes.Count < 2)
{
return;
}
var parent = nodes[0].ParentNode;
var list = new List<XmlElement>(nodes.Count);
foreach (XmlElement element in nodes)
{
list.Add(element);
}
list.Sort(Comparer);
foreach (XmlElement element in list)
{
// You can't remove in the other foreach, because it will break
// the childNodes collection
parent.RemoveChild(element);
parent.AppendChild(element);
}
}
public static int Comparer(XmlElement a, XmlElement b,str strAttributeName)
{
int aaa = int.Parse(a.Attributes["aa"].Value);
int aab = int.Parse(b.Attributes["aa"].Value);
int cmp = aaa.CompareTo(aab);
if (cmp != 0)
{
return cmp;
}
int ba = int.Parse(a.Attributes["b"].Value);
int bb = int.Parse(b.Attributes["b"].Value);
cmp = ba.CompareTo(bb);
return cmp;
}
在這裏,我想使a.Attributes["aa"].Value
爲a.Attributes[strAttributeName].Value
在我上面的代碼,使其更通用。我們該怎麼做呢?
請幫忙。
到底是怎麼做到的? 'a.Attributes [strAttributeName] .Value'。課程的類型應該是'string'而不是'str'。 – Rotem
對於初學者來說,你可以將'str strAttributeName'更改爲'string strAttributename' ...前者甚至不會編譯。你的問題到底是什麼?你不知道如何將一個字符串傳遞給一個函數? – tnw
'Comparer',你是否打算用'Compare'方法實現['IComparer'](http://msdn.microsoft.com/en-us/library/System.Collections.IComparer.aspx)接口?如果是這樣,沒有辦法改變進入它的參數的數量=/ – newfurniturey