当前位置:网站首页>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
边栏推荐
猜你喜欢

Protobuf use

Teach-Repeat-Replan: A Complete and Robust System for Aggressive Flight in Complex Environments

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

关于U盘数据提示RAW,需要格式化,数据恢复笔记

Window10版MySQL设置远程访问权限后不起效果

How to present your digital portfolio: suggestions from creative recruiters

C # control the camera, rotate and drag the observation script (similar to scenes observation mode)

Houdini terrain and fluid solution (simulated debris flow)

常用Markdown语法学习

Scrapy modifies the time in the statistics at the end of the crawler as the current system time
随机推荐
Houdini>建筑道路可变,学习过程笔记
Unity C single case mode learning review notes
Towords Open World Object Detection
C # use laida criterion (3) σ Criteria) reject abnormal data (.Net reject singular values in a group of data)
FUEL: Fast UAV Exploration using Incremental Frontier Structure and Hierarchical Planning
系统与软件安全研究(四)
自己封装unity的Debug函数
Encapsulate the debug function of unity
C#控制相机,旋转,拖拽观察脚本(类似Scenes观察方式)
NodeJS(二)同步读取文件和异步读取文件
IDEA快捷键
electron-builder打包报错:proxyconnect tcp: dial tcp :0: connectex
05数组的使用
VBA appelle SAP RFC pour réaliser la lecture et l'écriture des données
Understanding the Role of Individual Units in a Deep Neural Networks(了解各个卷积核在神经网络中的作用)
Solve the problem of deploying mysql8 in docker with correct password but unable to log in to MySQL
.NET 5 的新功能 What‘s new in .NET 5
Robust and Efficient Quadrotor Trajectory Generation for Fast Autonomous Flight
向量到一个平面的投影向量
平面定义-平面方程