当前位置:网站首页>Using lambda expression to solve the problem of C file name sorting (whether it is 100 or 11)
Using lambda expression to solve the problem of C file name sorting (whether it is 100 or 11)
2022-04-23 07:54:00 【Dake mountain man】
1、 The problem background :
Sometimes you need to sort the list of output files , If used directly List<T>.Sort() When sorting methods , encounter 9, 10,11 and 100 The question of sequencing , There will be 2.xxx Greater than 100.xxx ( among ,xxx Represents the suffix of the file ) And so on , This is obviously not what we want to think .
in addition , In the same set of files , If all in : NNN_1_ABC.xxx,NNN_2_ABC.xxx……NNN_NNNN_ABC.xxx The form such as ( among N Is the number ), here , stay NNN In the same case , We hope NNN_100 Than NNN_2 want “ Big ”.
For example, the following file list , It's direct use of List<T>.Sort() Method to sort :
D:\ChineseCalendar\AI_ style \OK\00_CoverFM cover \00FM_01+ Round disc + vertical .svg
D:\ChineseCalendar\AI_ style \OK\00_CoverFM cover \00FM_02_ Kraft paper background + Snake circle calendar + Happy New Year! + Large small text advertisements + General advertising .svg
………………// Here are some documents ( Next "//" Same as )……………………
D:\ChineseCalendar\AI_ style \OK\00_CoverFM cover \00FM_19+ Colored leaves + curve + Advertisement on the upper left, middle right .svg
………………// ……………………
D:\ChineseCalendar\AI_ style \OK\02\02_ Children _ Blue sky, white clouds, sunflowers, etc 12 monthly calendar .svg
………………// ……………………
D:\ChineseCalendar\AI_ style \OK\09\09_ Upper left small picture + Top right lateral view + Lower left text advertisement + Single month calendar with week serial number in the lower right x12_OK.svg
D:\ChineseCalendar\AI_ style \OK\10\10_ Iron clip + Patterns on both sides + middle 12 Monthly calendar + Bottom ad .svg
………………// ……………………
D:\ChineseCalendar\AI_ style \OK\100\100_HappyFall+(2x2+4x2)=12 monthly calendar .svg
………………// ……………………
D:\ChineseCalendar\AI_ style \OK\109\109_ Gradient labels +3x2=6 Calendar... Half a month .svg
D:\ChineseCalendar\AI_ style \OK\11\11_Calendar Round sticker _OK.svg
We hope to be 0-9-10-11...100-101..200...201..., Instead of the above .
2、 resolvent :
Post the code first :
/// <summary>
/// Yes List<string> File list sorting
/// </summary>
/// <param name="listSvgFiles">List<string> File list sorting </param>
/// <param name="isOrderByDesc"> Is it in reverse order? </param>
/// <returns> Sorting result </returns>
private static List<string> GetOrderListOfSvgFiles(List<string> listSvgFiles, bool isOrderByDesc)
{
// First use of Lambda The expressions are arranged in positive order
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 the order is reversed
if (isOrderByDesc)
{
orderedFile.Reverse();
}
return orderedFile;
}
explain : Bold text is used here to indicate the key points , meanwhile , If you have different needs , It can be modified according to the actual needs .
// Calling method :
List<string> orderedFileList = GetOrderListOfSvgFiles(listSvgFiles, isOrderByDesc);
Last , Let's go through time . This is a recent calendar sample for the next few thousand years , Hey !!

版权声明
本文为[Dake mountain man]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230625378365.html
边栏推荐
- Online Safe Trajectory Generation For Quadrotors Using Fast Marching Method and Bernstein Basis Poly
- C # control the camera, rotate and drag the observation script (similar to scenes observation mode)
- unity UGUI判断点击在UI上和3D物体上的解决方案
- Dropping Pixels for Adversarial Robustness
- 根据某一指定的表名、列名及列值来向前或向后N条查相关列值的SQL自定义标量值函数
- Nodejs (VI) sub process operation
- electron-builder打包报错:proxyconnect tcp: dial tcp :0: connectex
- Unity C single case mode learning review notes
- Idea shortcut
- 庄懂的TA笔记(七)<Lambert+Phong+Shadow+3EvColor+AO>
猜你喜欢

TA notes of Zhuang understand (VII) < Lambert + Phong + shadow + 3evcolor + Ao >

The page displays the current time in real time

VBA调用SAP RFC实现数据读取&写入

Towords Open World Object Detection

第五章 投资性房地产

Houdini地形与流体解算(模拟泥石流)

Scrapy modifies the time in the statistics at the end of the crawler as the current system time

About USB flash drive data prompt raw, need to format, data recovery notes

Houdini流体>>粒子流体导出到unity笔记

C#控制相机,旋转,拖拽观察脚本(类似Scenes观察方式)
随机推荐
Apache Hudi 如何加速传统的批处理模式?
How to present your digital portfolio: suggestions from creative recruiters
Solve the problem of deploying mysql8 in docker with correct password but unable to log in to MySQL
ES6使用递归实现深拷贝
C#使用拉依达准则(3σ准则)剔除异常数据(.Net剔除一组数据中的奇异值)
SQL针对字符串型数字进行排序
.NET 5 的新功能 What‘s new in .NET 5
Houdini > rigid body, rigid body breaking RBD
企业微信免登录跳转自建应用
Unity 获取一个文件依赖的资源
When using flash, the code ends automatically without an error, the connection cannot be maintained, and the URL cannot be accessed.
C# 多个矩形围成的多边形标注位置的问题
VBA calls SAP RFC to read & write data
H5 local storage data sessionstorage, localstorage
KCD_EXCEL_OLE_TO_INT_CONVERT报错SY-subrc = 2
SAP TR手动导入系统操作手册
C# 读取注册表
Scrapy 修改爬虫结束时统计数据中的时间为当前系统时间
Unity 获取一个资源被那些资源引用
事件系统(二)多播事件