当前位置:网站首页>利用Lambda表达式解决c#文件名排序问题(是100大还是11大的问题)
利用Lambda表达式解决c#文件名排序问题(是100大还是11大的问题)
2022-04-23 06:26:00 【大可山人】
1、问题背景:
有时需要对输出的文件列表进行排序,如直接使用List<T>.Sort()方法排序时,遇到9, 10,11和100排序的问题,往彺会出现2.xxx大于100.xxx (其中,xxx代表文件的后缀名) 等类似的情形,这明显不是我们要想的。
另外,在同一组文件中,假如都以: NNN_1_ABC.xxx,NNN_2_ABC.xxx……NNN_NNNN_ABC.xxx等形式(其中N为数字),此时,在NNN相同情况下,我们希望NNN_100比NNN_2 要“大”。
比如下面的文件列表,就是直接使用List<T>.Sort()方法排序产生的:
D:\ChineseCalendar\AI_样式\OK\00_CoverFM封面\00FM_01+圆形光盘+竖向.svg
D:\ChineseCalendar\AI_样式\OK\00_CoverFM封面\00FM_02_牛皮纸背景+蛇圈台历+新年快乐+大片小文字广告+普通广告.svg
………………// 这里还有一些文件(下"//"同)……………………
D:\ChineseCalendar\AI_样式\OK\00_CoverFM封面\00FM_19+彩色树叶+曲线+左中右上部广告.svg
………………// ……………………
D:\ChineseCalendar\AI_样式\OK\02\02_小朋友_蓝天白云向日葵等12月历.svg
………………// ……………………
D:\ChineseCalendar\AI_样式\OK\09\09_左上小图+右上横向大图+左下文字广告+右下含周序号单月历x12_OK.svg
D:\ChineseCalendar\AI_样式\OK\10\10_铁夹+两边花纹+中间12月年历+底部广告.svg
………………// ……………………
D:\ChineseCalendar\AI_样式\OK\100\100_HappyFall+(2x2+4x2)=12月历.svg
………………// ……………………
D:\ChineseCalendar\AI_样式\OK\109\109_渐变标签+3x2=6月历半年历.svg
D:\ChineseCalendar\AI_样式\OK\11\11_Calendar圆形贴纸_OK.svg
我们希望是0-9-10-11...100-101..200...201...,而不是上面那样。
2、解决方法:
先贴代码:
/// <summary>
/// 对List<string>文件列表排序
/// </summary>
/// <param name="listSvgFiles">List<string>文件列表排序</param>
/// <param name="isOrderByDesc">是否倒序</param>
/// <returns>排序结果</returns>
private static List<string> GetOrderListOfSvgFiles(List<string> listSvgFiles, bool isOrderByDesc)
{
//先利用Lambda表达式正序排列
List<string> orderedFile = (from s in listSvgFiles
let Split = s.Split('_')
let Str = Path.GetFileName(s)
where Str.IndexOf("FM") >= 0 || Regex.IsMatch(Str, @"\b\d+")
select s)
.OrderBy(x => int.Parse(Path.GetFileName(x).Split('_')[0].TrimEnd("FM".ToCharArray())))
.ThenBy(y => Path.GetFileName(y).Split('_')[1])
.ToList();
//如果逆序排列
if (isOrderByDesc)
{
orderedFile.Reverse();
}
return orderedFile;
}
说明:这里用加粗文字表示重点,同时,如你有不同需求,可以根据实际需要修改。
//调用方法:
List<string> orderedFileList = GetOrderListOfSvgFiles(listSvgFiles, isOrderByDesc);
最后,让我们来个时光穿越。这是最近做的未来几千年的年历样本,嘿嘿!!
版权声明
本文为[大可山人]所创,转载请带上原文链接,感谢
https://blog.csdn.net/johnsuna/article/details/121198671
边栏推荐
- 13. User and authority management
- SAP pi / PO rfc2restful publishing RFC interface is a restful example (proxy indirect method)
- 自定义时间格式(YYYY-MM-DD HH:mm:ss 星期X)
- 层次输出二叉树
- 5. Sql99 standard: internal connection and external connection
- 8.分页查询
- Configure NPM
- SAP 导出Excel文件打开显示:“xxx“的文件格式和扩展名不匹配。文件可能已损坏或不安全。除非您信任其来源,否则请勿打开。是否仍要打开它?
- SAP RFC_CVI_EI_INBOUND_MAIN BP主数据创建示例(仅演示客户)
- 12. Constraints
猜你喜欢
ABAP 7.4 SQL Window Expression
Custom time format (yyyy-mm-dd HH: mm: SS week x)
keytool: command not found
ABAP 从CDS VIEW 发布OData Service示例
Visualization Road (IX) detailed explanation of arrow class
反思 | Android 音视频缓存机制的系统性设计
SAP PI/PO rfc2Soap 发布rfc接口为ws示例
BTREE, B + tree and hash index
简易随机点名抽奖(js下编写)
ABAP 实现发布RESTful服务供外部调用示例
随机推荐
超级宝典&编程指南(红蓝宝书)-读书笔记
8. Paging query
对STL容器的理解
驼峰命名对像
Django uses MySQL database to solve error reporting
What are the total number of all courses of Mr. Tang Xiaoyang, who is very popular in CSDN (question mark)
js之作用域、作用域链、全局变量和局部变量
[self motivation series] what really hinders you?
Custom time format (yyyy-mm-dd HH: mm: SS week x)
js之排他思想及案例
How to judge whether a point is within a polygon (including complex polygons or a large number of polygons)
h5本地存储数据sessionStorage、localStorage
斐波拉去动态规划
反思 | 事件总线的局限性,组件化开发流程中通信机制的设计与实现
MySQL isolation level
手游性能优化
Reflect on the limitations of event bus and the design and implementation of communication mechanism in component development process
Understanding of STL container
8.分页查询
SAP PI / Po rfc2restful Publishing RFC interface as restful examples (proxy indirect)