1

im having a little trouble Ordering my files, i've researched on Stack overflow and tried all other methods but i keep getting the same problem

Thats my code:

public static List<Bitmap> CogerFotosAlamacenadas()
    {
        List<Bitmap> Lista = new List<Bitmap>();
        DirectoryInfo Directorio = new DirectoryInfo(Environment.CurrentDirectory + "\\Almacenamiento");
        FileInfo[] ListaDeFotos = Directorio.GetFiles("*.bmp");

        Array.Sort(ListaDeFotos, delegate (FileInfo x, FileInfo y)
        {
            return string.Compare(x.Name, y.Name);
        });

        foreach (FileInfo foto in ListaDeFotos)
        {
            Image PlaceHolder = Image.FromFile(foto.FullName);

            Lista.Add((Bitmap)PlaceHolder);

        }


        return Lista;
    }

I have a serie of photos named: "Foto" + numberFrom0To300 + "bmp";

after this code is aplied my list get's the photos ordered by 0_10_100_101_102...

Already tried the default order from .GetFiles() this code and another one found in stack overflow whitout usin array.sort i always get the same result hat odd order number

but i have to order them 0,1,2,3,4... at all cost

does ayone have a good idea how to control it?

Mafii
  • 7,227
  • 1
  • 35
  • 55
Elan Sanchez
  • 110
  • 1
  • 10
  • 1
    You're going to have to be clearer: what is the issue? What's it doing wrong? – rory.ap Apr 12 '16 at 12:17
  • 1
    "after this code is aplied my list get's the photos ordered by 0_10_100_101_102..." "but i have to order them 0,1,2,3,4... at all cost" i think its clear enough to not get a instantaneous downvote – Elan Sanchez Apr 12 '16 at 12:19
  • I asked you to *clarify* not to quote from your question. – rory.ap Apr 12 '16 at 12:22
  • i dont speak english so maybe i didnt explain myself good enough, the thing is that my program thinks that photo101.bmp is smaller than photo2.bmp and i dont want them to order like this i want them to order 1,2,3,4,5 instead of 1,10,100,101 and so on – Elan Sanchez Apr 12 '16 at 12:30
  • 1
    Possible duplicate of [Sorting a List of Strings numerically (1,2,...,9,10 instead of 1,10,2)](http://stackoverflow.com/questions/4788227/sorting-a-list-of-strings-numerically-1-2-9-10-instead-of-1-10-2) – disappointed in SO leadership Apr 12 '16 at 12:34

2 Answers2

1

Quick (but may be dirty - interop required) solution is to sort in different way:

using System.Runtime.InteropServices;
...

[DllImport("shlwapi.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
private static extern int StrCmpLogicalW(string x, string y);

...

// change your current Array.Sort to this one
Array.Sort(ListaDeFotos, (left, right) => StrCmpLogicalW(left.Name, right.Name));

The trick is that string.Compare compares in lexicographical way (and so "10" < "9") when StrCmpLogicalW provides a kind of logical sort ("10" > "9")

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • Thx for the answer! used the other answer because it didnt had to import and was easyer to me but thx a lot! might be useful for someone else! – Elan Sanchez Apr 12 '16 at 12:50
1

You are comparing strings which is using a lexicographical order, you want to order by the number. Then you have to always parse the relevant substring. You can use LINQ:

FileInfo[] orderedPhotos = Directorio.EnumerateFiles("*.bmp")
    .Where(f => f.Name.Length > "Foto.bmp".Length)
    .Select(f => new { 
        File = f, 
        Number = System.IO.Path.GetFileNameWithoutExtension(f.Name).Substring("Foto".Length)
    })
    .Where(x => x.Number.All(Char.IsDigit))
    .Select(x => new { 
        x.File, 
        Integer = int.Parse(x.Number)
    })
    .OrderBy(x => x.Integer)
    .Select(x => x.File)
    .ToArray();
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • Tested and working properly, items are ordered as i wanted – Elan Sanchez Apr 12 '16 at 12:47
  • @ElanSanchezOrtiz: you're welcome. If you want to reverse the order use `OrderByDescending`. If you want to search fotos with a given number (or range) use `Where(x => x.Integer == 123)` – Tim Schmelter Apr 12 '16 at 12:58