【发布时间】:2018-05-27 08:43:09
【问题描述】:
我有以下课程:
public class Document
{
public string DocumentSection { get; set; }
public string DocumentName { get; set; }
}
我想根据 DocumentSection 属性订购以下列表:
List<Document> documents = new List<Document>();
documents.Add(new Document { DocumentSection = "Section One", DocumentName = "doc1" });
documents.Add(new Document { DocumentSection = "Section Two", DocumentName = "doc1123" });
documents.Add(new Document { DocumentSection = "Section Three", DocumentName = "doc113" });
documents.Add(new Document { DocumentSection = "Section Four", DocumentName = "doc123" });
documents.Add(new Document { DocumentSection = "Section Five", DocumentName = "doc11" });
理论上我知道我应该实现 IComparer 来获得它,但这就是困难所在,我不太确定如何在一般水平上实现它......实现这一目标的最佳解决方案是什么订购?
【问题讨论】:
-
是
DocumentSection总是带有第一节,第二节,...等,然后一种方法是将一,二...(修剪节字符串)更改为数字和命令。另一个解决方案是,不是将文档部分作为字符串,而是将其作为数值并对其进行排序。如果需要该属性在绑定视图上显示文本,则编写一个转换器将数值更改为文本(带有所需的前后修复字符串。) -
是的,总是这样......问题是我根本无法更改模型结构
-
“第一节”、“第二节”、“第三节”……等等……“一百节”……很糟糕……但我必须处理现在这个真实的案例场景......一个大的 switch 语句将完成这项工作......或 Dictionary
{"One", 1"} {"Two", 2 "} 等......但我有手写所有这些情况......如果有更好的选择,我很好奇 -
如果部分是 1001,
DocumentSection的 具体 值是多少?如果是 10914 呢? -
@RaduOlteanu 检查我的代码,这是 支持所有数字
....,-2,-1,0,1,2,3,....并且不需要在单词中写入任何数字...。检查一下,我确定这对你有好处。