-7

I'm making a program which gets me a list, but the list is not in order. I need to list to be in order like...

1.txt
2.txt
3.txt

However, it's coming out like

2.txt
1.txt
3.txt

All in random order. I am using

foreach (var file in d.GetFiles("*.txt"))
{                 
    tosend = tosend + file.Name + "\n";
}

I don't want a single string. After it gets the file name it's going to readtext of other files its set and add it. So like [1.txt] Text

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
xxLoganxx
  • 63
  • 1
  • 8

4 Answers4

4

Use LINQ:

foreach (var file in d.GetFiles("*.txt").OrderBy(x => x.Name).ToList())
{
    ...
}

If the files will always be a number.txt you can use

foreach (var file in d.GetFiles("*.txt").OrderBy(x => int.Parse(x.Name.Substring(0,x.Name.IndexOf(".txt"))).ToList())
{
    ...
}

You will want to add some error checking.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
jkozlowski
  • 178
  • 10
1

Or you can sort the array:

string[] files = d.GetFiles("*.txt")
Array.Sort(files, StringComparer.InvariantCulture);
foreach (var file in files)
{                 
   tosend = tosend + file.Name + "\n";
}
NiVeR
  • 9,644
  • 4
  • 30
  • 35
  • Doesn't work file string[] files is not valid with system.io – xxLoganxx Apr 20 '17 at 19:48
  • Hmm..I don't have a way to try it right now, but according to the documentation (https://msdn.microsoft.com/en-us/library/07wt70x2(v=vs.110).aspx#Examples) it should work. – NiVeR Apr 20 '17 at 19:54
0

Use Linq

// top of code file
using System.Linq;

In method:

var files = d.GetFiles("*.txt").OrderBy(file => file.Name).ToList();

or as a string as per your example

string tosend = string.Join("\n", d.GetFiles("*.txt")
         .Select(file => file.Name)
         .OrderBy(x => x));

This joins the returned ordered list of file names using \n as a separator.

Igor
  • 60,821
  • 10
  • 100
  • 175
  • 1
    I don't want to ToList though. Because there isnt going to be one thing after the tosend. It has to be other things so its all on one line. – xxLoganxx Apr 20 '17 at 19:43
  • It looks like OP wants a list in a text string not a `List` object. – MotKohn Apr 20 '17 at 19:45
  • @LoganRisen - I added a second example of how you can use linq. Here you get a single string of the file names joined (in order) by `"\n"`. – Igor Apr 20 '17 at 19:47
  • @LoganRisen - see Eric's comment though, sorting alphabetically does not work very well when you deal with numbers. – Igor Apr 20 '17 at 19:48
0

If you are looking for Natural sort i.e.

  1.txt
  9.txt
 10.txt 

you can use Interop as a quick (and may be a dirty) solution (see Natural Sort Order in C# for details)

using System.Runtime.InteropServices;

...

[DllImport("shlwapi.dll", CharSet = CharSet.Unicode)]
private static extern int StrCmpLogicalW(string left, string right);

...

// Obtain files
var files = d.GetFiles();

// Sort files in natural order
Array.Sort(files, (left, right) => StrCmpLogicalW(left.Name, right.Name));

// combine files' names into the string
string tosend = string.Join("\n", files);
Community
  • 1
  • 1
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • @BJ Myers: `StrCmpLogicalW` is the function *File Explorer* uses to sort files, but you are quite right: *Interop* is often a dirty solution even if it's quick one. – Dmitry Bychenko Apr 20 '17 at 20:59