0

I have problem in sorting an array in ascending form and i don't know how to fix it.

string[] filePath = Directory.GetFiles(fbdialog.SelectedPath.ToString(), "*", SearchOption.AllDirectories);
Array.Sort(filePath);

Here are the values of an filePath.

"C:\\Documents and Settings\\20110909\\DAR-AAP070127-20110909.ods"
"C:\\Documents and Settings\\20110909\\DAR-ALA061452-09050909.xls"
"C:\\Documents and Settings\\20110819\\DAR-AAP070127-20110819.xls"

I want to look like this..

"C:\\Documents and Settings\\20110909\\DAR-AAP070127-20110909.ods"
"C:\\Documents and Settings\\20110819\\DAR-AAP070127-20110819.xls"
"C:\\Documents and Settings\\20110909\\DAR-ALA061452-09050909.xls"

Thanks in Advance.

3
  • 7
    Can you explain the logic behind your order? Commented Sep 23, 2011 at 9:27
  • @Snowbear I think by filename Commented Sep 23, 2011 at 9:29
  • I want to sort it by EmpNo(AAP070127). Commented Sep 23, 2011 at 9:40

4 Answers 4

1

To sort by "EmpNo" e.g. "AAP070127":

string[] sortedFiles = Directory
    .GetFiles(fbdialog.SelectedPath, "*", SearchOption.AllDirectories)
    .OrderBy(n => Path.GetFileName(n).Split('-')[1])
    .ToArray();

Update

And without Linq as you mention using C# 2.0. The following uses a custom comparer to compare only the "EmpNo" codes in your file names. The solution expects your file names to be well-formed i.e. for them to contain an "EmpNo" in the format of your example file names.

    [Test]
    public void Sort()
    {
        string[] files = Directory
            .GetFiles(fbdialog.SelectedPath, "*", SearchOption.AllDirectories);

        Array.Sort(files, new EmpNoFileComparer());
    }

    private class EmpNoFileComparer : IComparer<string>
    {
        public int Compare(string x, string y)
        {
            string empNoX = Path.GetFileName(x).Split('-')[1];
            string empNoY = Path.GetFileName(y).Split('-')[1];

            return empNoX.CompareTo(empNoY);
        }
    }
Sign up to request clarification or add additional context in comments.

7 Comments

It's better to use DirectoryInfo.GetFiles() and don't use SelectedPath.ToString()
@abatishchev I'm using SelectedPath.ToString().
@Sam I'm not sure I follow you?
@Sam What problem are you having?
@Sam I have provided an alternative answer which does not use Linq, as this is only available from C# 3.0 and you appear to be using C# 2.0. I would recommend upgrading if at all possible. C#2.0 is now quite old.
|
1

Sort by filename:

var result = filePath.OrderBy(p => Path.GetFileName(p));

Comments

1

Here's a version using some LINQ. Note also that if you only need the file names, not the files themselves, then DirectoryInfo.GetFiles() can be used rather than Directory.GetFiles().

var filePaths = new DirectoryInfo(fbdialog.SelectedPath.ToString())
    .GetFiles("*", SearchOption.AllDirectories)
    .OrderBy(f => f.Name)
    .Select(f => f.FullName);

Comments

0
using System.Linq; // requires .NET 3.5+

IEnumerable<string> r =
    new DirectoryInfo(fbdialog.SelectedPath) // no need for ToString()
        .GetFiles("*", SearchOption.AllDirectories)
        .Select(f => f.Name) // DAR-ALA061452-09050909.xls
        .Select(f => f.Substring(4, 9)) // ALA061452
        .OrderBy(f => f);

or shorter:

new DirectoryInfo(fbdialog.SelectedPath)
    .GetFiles("*", SearchOption.AllDirectories)
    .OrderBy(f => f.Name.Substring(4, 9));

7 Comments

this could eventually work but not always, I think he should use System.Path.GetFileName() to isolate file name and sort by filename only ignoring the directory name...
@DavidePiras: First option for case if OP wants to order by date part.
The reason for downvote? If you don't understand something - ask.
I want to order by EmpNo(AAP070127).
@Sam: Why don't use C# 2010 Express Edition? It's free as well and much more modern
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.