当前位置:网站首页>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
边栏推荐
- Houdini terrain and fluid solution (simulated debris flow)
- Dictionary & lt; T1,T2&gt; Sorting problem
- 【Unity VFX】VFX特效入门笔记-火花制作
- 庄懂的TA笔记(七)<Lambert+Phong+Shadow+3EvColor+AO>
- Apache Hudi 如何加速传统的批处理模式?
- 将单行文字自动适应到目标矩形框内
- 第四章 无形资产
- Houdini > rigid body, rigid body breaking RBD
- Apache Hudi 如何加速传统的批处理模式?
- The problem of exporting excel form with wireframe and internal spacing of form by using web form
猜你喜欢
Understanding the role of individual units in a deep neural networks
The page displays the current time in real time
Robust and Efficient Quadrotor Trajectory Generation for Fast Autonomous Flight
Protobuf use
平面定义-平面方程
How does Apache Hudi accelerate traditional batch mode?
第四章 无形资产
Window10版MySQL设置远程访问权限后不起效果
【NLP笔记】CRF原理初探
Apache Hudi 如何加速传统的批处理模式?
随机推荐
快速排序
[NLP notes] preliminary study on CRF principle
Houdini地形与流体解算(模拟泥石流)
SVG中年月日相关的表达式
Nodejs (four) character reading
Houdini>刚体, 刚体破碎RBD
Idea shortcut
给定区段范围内字符串自生成代码
第四章 无形资产
UnityShader基础
Unity gets the resources that a file depends on
IDEA快捷键
STO With Billing 跨公司库存转储退货
Unity获取真实地理地图应用Terrain笔记
Solve the problem of deploying mysql8 in docker with correct password but unable to log in to MySQL
How to present your digital portfolio: suggestions from creative recruiters
第五章 投资性房地产
FUEL: Fast UAV Exploration using Incremental Frontier Structure and Hierarchical Planning
SampleCameraFilter
解决在docker中部署mysql8, 密码正确但无法登陆MySQL问题