对列表进行排序通过匹配的枚举名称值
有点不确定如何做到这一点。我需要以与文件服务器上显示的顺序不同的顺序显示文件列表。
我在想一种方法是按匹配的枚举名称值对字符串列表进行排序。
假设我有一个完整的字符串列表:
List<string> filenames = new List<string>();
并且我有一个相关的枚举以按特定顺序显示文件:
public enum ProcessWorkFlowOrder
{
File1,
File3,
File2
}
列表中的“文件名”字符串值将与枚举的名称完全匹配。
按匹配的枚举值对文件名列表进行匹配和排序的最佳方法是什么?
A little unsure of how to do this. I need to display a list of files in a different order than they are presented on our file server.
I was thinking one way would be to order a list of strings by a matched enum's name value.
Let's say I have a full list of strings:
List<string> filenames = new List<string>();
And I have a related enum to show the files in a certain order:
public enum ProcessWorkFlowOrder
{
File1,
File3,
File2
}
The "filenames" string value in the List will match exactly the Enum's name.
What is the best way to match and order the FileNames list by its matched enum value?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果文件名完全匹配,我不会使用枚举,而是使用另一个保留顺序的列表。
Join
保留第一个序列的顺序,因此允许您使用它来对第二个序列进行排序。If the filenames match exactly, I wouldn't use an enum but rather another order-preserving list.
Join
preserves the order of the first sequence and will therefore allow you to use it to order the second.如果您已经有了枚举,并且无法像安东尼建议的那样将其更改为字符串列表,则可以使用以下命令:
If you already have the enum and you can't change it to a list of string like Anthony suggests, you can use this:
怎么样?
但是,我不确定维护这个枚举是最好的方法,最好编写一个函数来定义顺序算法,如果枚举不包含文件名怎么办。
使用 TryParse 编辑
您将使用什么规则来构造枚举或顺序保留列表,或者顺序是否纯粹是任意的?
how about?
However, I'm not sure that maintaining this enum is the best approach, better to write a function to define the order algorithm, what if enum does not contain the file name.
EDIT with TryParse
What rules would you use to construct the enum or an order preserving list or, is the order purely arbitary?