0

Attempting to sort files numerically. A list of files:

1.jpg 2.jpg 3.jpg 4. jpg 10.jpg 11.jpg 20.jpg

this list would be ordered like so:

1.jpg 10.jpg 11.jpg 2.jpg 20.jpg 3.jpg 4.jpg

    private void button1_Click(object sender, EventArgs e)
    {   
        string x = txtPath.Text;
        string[] path = Directory.GetFiles(x);

        string[] filePaths = path;
        foreach (string element in filePaths)
        {
            lb1.Items.Add(Path.GetFileName(element));
        }

        lb1.Sorted = true;
    }
Jimmy K
  • 89
  • 1
  • 9
  • possible duplicate of [Sorting strings containing numbers in a user friendly way](http://stackoverflow.com/questions/1022203/sorting-strings-containing-numbers-in-a-user-friendly-way) – Josh Anderson Mar 13 '14 at 19:37

3 Answers3

0

They are sorting this way because they are string, and try sort lexicographically.

Try something like this:

string[] line = { "file1.jpg", "file2.jpg", "file10.jpg"};
Regex r = new Regex("/d+");
var result = line.OrderBy(x => r.Match(x).Value);

if you want a string, use this:

string[] result = line.OrderBy(x => r.Match(x).Value).ToArray<string>();

Adapt this to your needs.

the output is:

file1

file2

file10

Oscar Bralo
  • 1,912
  • 13
  • 12
0

You can extract the number of the picture and parsed as some int number, sorted it,and after that put in place extensions.

Mihai8
  • 3,113
  • 1
  • 21
  • 31
0

ls -v *.jpg

Check the ls man page.

ewing
  • 21
  • 1