如何实现 IComparable 对数字和非数字字符串进行排序。
首先,我想在“列表”中获取最小值和最大值。
它要求我实现 ICompareable。" 至少一个对象必须实现 IComparable"
谁能帮我?这是我的代码:
// setup
string filePath1 = Directory.GetCurrentDirectory().ToLower().Replace(@"\bin\debug", string.Empty)
+ @"\SampleData\products.xml";
// load XML
var xdoc1 = XElement.Load(filePath1);
//list
var elements = xdoc1.Descendants("ProductName").OrderBy(x => x.Value);
//output
Console.WriteLine("Min- " + elements.Min().Value);
Console.WriteLine("Max- " + elements.Max().Value);
这是我的 XML 文件:
<Products>
<ProductName>9954</ProductName>
<ProductName>987</ProductName>
<ProductName>197</ProductName>
<ProductName>56</ProductName>
<ProductName>rr45</ProductName>
<ProductName>ad4</ProductName>
<ProductName>7</ProductName>
<ProductName>69dd</ProductName>
<ProductName>69</ProductName>
<ProductName>197abc</ProductName>
</Products>