-1

enter image description here

I wan't to sort files directory.I have got a lot of file directions. Like: "C:\Users\user\Desktop\programlama\destin\ankara\c11.txt" "C:\Users\user\Desktop\programlama\destin\ankara\a12.txt" "C:\Users\user\Desktop\programlama\destin\ankara\b11.txt"

But it must sort by file names. How can i do that?

Result i want:
a12 at top b11 in the middle c11 at last.

Here code sample.

FolderBrowserDialog fbd = new FolderBrowserDialog();

fbd.ShowDialog();
kaynak = fbd.SelectedPath; 
dnm = Directory.GetFiles(kaynak,ftyp, SearchOption.AllDirectories)
                .Select(Path.GetFileName)
                .ToArray();
dsd = Directory.GetFiles(kaynak, ftyp, SearchOption.AllDirectories);
maccettura
  • 10,514
  • 3
  • 28
  • 35

3 Answers3

1

One simple way to do it is using a labmda expression with Array.Sort (Please note I've changed the paths a little bit to show it's irrelevant to the sort:

var files = new string[]
{
    @"C:\Users\user\Desktop\programlama\destin\ankara\c11.txt",
    @"C:\Users\user\Desktop\programlama\destin\a12.txt",
    @"C:\Users\user\Desktop\programlama\ankara\b11.txt"
};


Array.Sort(files, (s1, s2) => Path.GetFileName(s1).CompareTo(Path.GetFileName(s2)));

Result:

C:\Users\user\Desktop\programlama\destin\a12.txt
C:\Users\user\Desktop\programlama\ankara\b11.txt
C:\Users\user\Desktop\programlama\destin\ankara\c11.txt

You can see a live demo on rextester.

Zohar Peled
  • 79,642
  • 10
  • 69
  • 121
0

Or you can use Linq to sort by path:

var files = new List<string>
{
   @"C:\Users\user\Desktop\programlama\destin\ankara\c11.txt",
   @"C:\Users\user\Desktop\programlama\destin\ankara\a12.txt",
   @"C:\Users\user\Desktop\programlama\destin\ankara\b11.txt"
};

files = files.OrderBy(f => f).ToList();

Result

C:\Users\user\Desktop\programlama\destin\ankara\a12.txt
C:\Users\user\Desktop\programlama\destin\ankara\b11.txt
C:\Users\user\Desktop\programlama\destin\ankara\c11.txt
Antonio
  • 21
  • 1
  • 5
  • 1
    That would: 1) leave the original List of strings unsorted, 2) return an IEnumerable and 3) sort by the entire path. – Zohar Peled Apr 09 '18 at 21:22
  • Edited. I don't find it clear from the post if he wants to change the original list or not. Anyway, thanks for the comment :) – Antonio Apr 09 '18 at 21:29
  • Better, but still needs to sort by file name only (and the original question was about an array, not a list, but that's just nitpicking :-)) – Zohar Peled Apr 09 '18 at 21:32
  • @ZoharPeled Ouch! Totally misread the question. Good point. I'll give your answer an upvote. Thanks twice! – Antonio Apr 09 '18 at 21:37
  • Don't worry about it, that happens to everyone... I'll bet even Jon Skeet have a deleted answer for that very reason :-) – Zohar Peled Apr 09 '18 at 21:39
-1

Something like:

filesArray.Select(q=> new { FullPath = q, FileName = Path.GetFileName(q)}).OrderBy(q=> q.FileName).Select(q=>q.FullPath);
  • I am not sure your code compiles. You might want to fix the formatting and double check. – maccettura Apr 09 '18 at 21:19
  • @maccettura I'm not sitting in front of my pc right now, i typed the solution from my head, but it should be ok if you import Sytem.Linq namespace. – Alexander Powolozki Apr 09 '18 at 21:23
  • It solves problem too. Thanks so much. – Ahmet Ali Soysal Apr 09 '18 at 21:23
  • I would take a look again, specifically after your `Path.GetFileName(q)`... – maccettura Apr 09 '18 at 21:23
  • 1
    I almost compiles, just replace the `;` with `,`. But it will not sort the original array, only return a sorted IEnumerable (and have poor performance, since it must iterate through the array 3 times). – Zohar Peled Apr 09 '18 at 21:24
  • @maccettura: topic starter can fine tune solution himself / herself, i only showed a possible solution, i' m on my smartphone with no pc and no compiler/debugger now, that's why the mistakes. – Alexander Powolozki Apr 09 '18 at 21:33
  • @AlexanderPowolozki I understand, but answers with two words and code that does not compile aren't very _good_ answers. – maccettura Apr 09 '18 at 21:34
  • 1
    I used to try and answer questions from my smartphone. Turns out not to be such a smartphone after all - it's really hard to do it right like this. My advise - don't do that. reading the question is hard enough, answering it correctly is even harder, especially when you have to include code in your answer. – Zohar Peled Apr 09 '18 at 21:37