根据文件名(SQL文件)使用自然排序对FileInfo []进行排序 [英] Sorting a FileInfo[] based using Natural Sorting on the filename (SQL files)
问题描述
这是我的问题:
我正在尝试运行一堆使用如下命名系统的SQL文件:
I am trying to run a bunch of SQL files that are using a naming system that looks like this:
1-createtable.sql
1-createtable.sql
2-modifytable.sql
2-modifytable.sql
.....
10-createanothertable.sql
10-createanothertable.sql
以此类推.
问题是,我用来获取文件的代码使用文件名作为字符串对它们进行排序,从而导致文件被排序,如下所示:
The problem is, the code that I am using to get the files orders them using the filename as a string which results in the files being ordered like:
10-createanothertable.sql
10-createanothertable.sql
1-createtable.sql
1-createtable.sql
2-modifytable.sql
2-modifytable.sql
我需要按-"之前的数值运行它们.
I need them ran by the numeric value before the "-".
我已经阅读并知道这被称为自然排序,但是想要在前进的道路上找到某种方向.
I have read up and know this is called natural sorting but would like some sort of direction on the way to go.
推荐答案
快速方法:
IEnumerable<FileInfo> fileInfosOrdered = fileInfos.OrderBy(fileInfo => int.Parse(fileInfo.Name.Substring(0, fileInfo.Name.IndexOf('-'))));
缓慢但彻底的方法:
阅读Sam Leach评论中的链接.
Read the links in the comment by Sam Leach.
这篇关于根据文件名(SQL文件)使用自然排序对FileInfo []进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!