且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

排序字符串Arry

更新时间:2023-02-23 09:20:28

您好,

除了两个列表之间的数据不一致之外(请参阅9.bmp.第一个列表具有2 \\,第二个列表具有4 \\-这会严重影响排序,正如我现在将要解释的那样.)

您稍微误解了排序算法的功能.它的作用是比较每个字符串开头的每个字母,以查看哪个字母具有更大"的值.因此(获取您的第一组数据),就在最后一个双后退序列之前,它有一个数字.看到的顺序是:
1 \\ 1
2 \\ 2
2 \\ 9
3 \\ 3
4 \\ 4

哪个是对的.但是,您要查找的是按文件名而不是其完整路径排序的文件.例如

1 \\ 1
2 \\ 2
3 \\ 3
4 \\ 4
2 \\ 9

但是如前所述,字符串从一开始就按字符进行排序,因此这里的第二个结果是不可能的.要进行某种搜索,实际上是您有一个选择.创建FileInfo对象的列表,然后按文件名排序.希望以下代码可以工作,但可能并非没有错误(未经测试):

Hi there,

Aside from your data being inconsistent between your two lists (see 9.bmp. First list has 2\\, 2nd list has 4\\ - this affects the ordering significantly, as I will now explain.)

You have slightly misunderstood what the sort algorithm is doing. What it does is compare each letter from the start of each string to see which has a "greater" value. So (taking your first set of data), just before the last set of double backslahes it has a number. The sequence seen is:
1\\1
2\\2
2\\9
3\\3
4\\4

Which is correct. However, what you were looking for was the files to be sorted by the file name, not its full path. E.g.

1\\1
2\\2
3\\3
4\\4
2\\9

But as stated, the strings are sorted character by character from the start so the second result here is impossible. To do the sort of searching you are after you have one option really. Create a list of FileInfo objects and then sort by file name. Hopefully the following code works but it may not be bug free (untested):

string[] fileEntries = Directory.GetFiles(Application.StartupPath + "/TrainedFaces/", "*.bmp", SearchOption.AllDirectories);
List<FileInfo> FileInfos = fileEntries.ToList().ConvertAll(x => new FileInfo(x)).ToList();
FileInfos = FileInfos.OrderBy(x => x.Name).ToList();
foreach (FileInfo AFileInfo in FileInfos)
{
    LoadFaces = AFileInfo.Name;
    trainingImages.Add(new Image<Gray, byte>(AFileInfo.FullName));
}



希望这会有所帮助,
Ed



Hope this helps,
Ed


var fileEntries =来自Directory.GetFiles(Application.StartupPath +"/TrainedFaces/","* .bmp",SearchOption.AllDirectories)中的f,让fi =新的FileInfo(f) orderby fi.Name选择fi.FullName;


终于我找到了我自己,感谢对这个问题有帮助的人们
var fileEntries = from f in Directory.GetFiles(Application.StartupPath + "/TrainedFaces/", "*.bmp", SearchOption.AllDirectories) let fi = new FileInfo(f) orderby fi.Name select fi.FullName;


Finally I found It my Self ,Thank For Peoples who are Help in this Problem