当前位置:网站首页>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
边栏推荐
- 将单行文字自动适应到目标矩形框内
- webflux文件上传下载
- C operation registry full introduction
- Understanding the role of individual units in a deep neural networks
- Houdini > variable building roads, learning process notes
- 庄懂的TA笔记(六)<FakeEnvReflect && 生锈,锈迹效果>
- SampleCameraFilter
- SAP STO With Billing流程与配置
- Encapsulate the debug function of unity
- Scrapy modifies the time in the statistics at the end of the crawler as the current system time
猜你喜欢
Protobuf 使用
Mongodb 启动警告信息处理
向量到一个平面的投影向量
Houdini>建筑道路可变,学习过程笔记
Zhuang understand's TA notes (VI) < fakeenvreflect & rust, rust effect >
VBA調用SAP RFC實現數據讀取&寫入
平面定义-平面方程
The page displays the current time in real time
三分钟教你用Houdini流体>>解算粒子流体水滴
TA notes of Zhuang understand (VII) < Lambert + Phong + shadow + 3evcolor + Ao >
随机推荐
企业微信免登录跳转自建应用
IDEA快捷键
颜色转换公式大全及转换表格(31种)
索引被锁数据无法写入ES问题处理
Online Safe Trajectory Generation For Quadrotors Using Fast Marching Method and Bernstein Basis Poly
Apache Hudi 如何加速传统的批处理模式?
Weblux file upload and download
A programmer who works four hours a day
命令行参数传递库argparse的使用
C SVG path parser of xamarin version
MySQL8.0 安装/卸载 教程【Window10版】
Unity ugui determines the solution of clicking on the UI and 3D objects
Understanding the role of individual units in a deep neural networks
Use of command line parameter passing library argparse
C# 读取注册表
The page displays the current time in real time
Towords Open World Object Detection
Read and modify the JSON file under the resource folder
SQL user-defined scalar value function that looks up relevant column values n times forward or backward according to a specified table name, column name and column value
事件管理之一