linq - C# getting sorted list of images -


this question has answer here:

i'm trying sorted list of images paths strings in praticular directory. directory contains 43 images named numbers: "1.png", "2.png",and on.

this code:

  var sorted = directory.getfiles(directory.getcurrentdirectory(), "*.png").orderby(f => f);              foreach (string img in sorted)    {    console.writeline(img);   }  

the result i'm getting

c:\users\itapi\onedrive\מסמכים\visual studio 2013\projects\draw\draw\bin\debug\1.png c:\users\itapi\onedrive\מסמכים\visual studio 2013\projects\draw\draw\bin\debug\10.png c:\users\itapi\onedrive\מסמכים\visual studio 2013\projects\draw\draw\bin\debug\11.png c:\users\itapi\onedrive\מסמכים\visual studio 2013\projects\draw\draw\bin\debug\12.png c:\users\itapi\onedrive\מסמכים\visual studio 2013\projects\draw\draw\bin\debug\13.png c:\users\itapi\onedrive\מסמכים\visual studio 2013\projects\draw\draw\bin\debug\14.png c:\users\itapi\onedrive\מסמכים\visual studio 2013\projects\draw\draw\bin\debug\15.png c:\users\itapi\onedrive\מסמכים\visual studio 2013\projects\draw\draw\bin\debug\16.png c:\users\itapi\onedrive\מסמכים\visual studio 2013\projects\draw\draw\bin\debug\17.png c:\users\itapi\onedrive\מסמכים\visual studio 2013\projects\draw\draw\bin\debug\18.png c:\users\itapi\onedrive\מסמכים\visual studio 2013\projects\draw\draw\bin\debug\19.png c:\users\itapi\onedrive\מסמכים\visual studio 2013\projects\draw\draw\bin\debug\2.png c:\users\itapi\onedrive\מסמכים\visual studio 2013\projects\draw\draw\bin\debug\20.png c:\users\itapi\onedrive\מסמכים\visual studio 2013\projects\draw\draw\bin\debug\21.png c:\users\itapi\onedrive\מסמכים\visual studio 2013\projects\draw\draw\bin\debug\22.png c:\users\itapi\onedrive\מסמכים\visual studio 2013\projects\draw\draw\bin\debug\23.png c:\users\itapi\onedrive\מסמכים\visual studio 2013\projects\draw\draw\bin\debug\24.png c:\users\itapi\onedrive\מסמכים\visual studio 2013\projects\draw\draw\bin\debug\25.png c:\users\itapi\onedrive\מסמכים\visual studio 2013\projects\draw\draw\bin\debug\26.png c:\users\itapi\onedrive\מסמכים\visual studio 2013\projects\draw\draw\bin\debug\27.png c:\users\itapi\onedrive\מסמכים\visual studio 2013\projects\draw\draw\bin\debug\28.png c:\users\itapi\onedrive\מסמכים\visual studio 2013\projects\draw\draw\bin\debug\29.png c:\users\itapi\onedrive\מסמכים\visual studio 2013\projects\draw\draw\bin\debug\3.png c:\users\itapi\onedrive\מסמכים\visual studio 2013\projects\draw\draw\bin\debug\30.png c:\users\itapi\onedrive\מסמכים\visual studio 2013\projects\draw\draw\bin\debug\31.png c:\users\itapi\onedrive\מסמכים\visual studio 2013\projects\draw\draw\bin\debug\32.png c:\users\itapi\onedrive\מסמכים\visual studio 2013\projects\draw\draw\bin\debug\33.png c:\users\itapi\onedrive\מסמכים\visual studio 2013\projects\draw\draw\bin\debug\34.png c:\users\itapi\onedrive\מסמכים\visual studio 2013\projects\draw\draw\bin\debug\35.png c:\users\itapi\onedrive\מסמכים\visual studio 2013\projects\draw\draw\bin\debug\36.png c:\users\itapi\onedrive\מסמכים\visual studio 2013\projects\draw\draw\bin\debug\37.png c:\users\itapi\onedrive\מסמכים\visual studio 2013\projects\draw\draw\bin\debug\38.png c:\users\itapi\onedrive\מסמכים\visual studio 2013\projects\draw\draw\bin\debug\39.png c:\users\itapi\onedrive\מסמכים\visual studio 2013\projects\draw\draw\bin\debug\4.png c:\users\itapi\onedrive\מסמכים\visual studio 2013\projects\draw\draw\bin\debug\40.png c:\users\itapi\onedrive\מסמכים\visual studio 2013\projects\draw\draw\bin\debug\41.png c:\users\itapi\onedrive\מסמכים\visual studio 2013\projects\draw\draw\bin\debug\42.png c:\users\itapi\onedrive\מסמכים\visual studio 2013\projects\draw\draw\bin\debug\43.png c:\users\itapi\onedrive\מסמכים\visual studio 2013\projects\draw\draw\bin\debug\5.png c:\users\itapi\onedrive\מסמכים\visual studio 2013\projects\draw\draw\bin\debug\6.png c:\users\itapi\onedrive\מסמכים\visual studio 2013\projects\draw\draw\bin\debug\7.png c:\users\itapi\onedrive\מסמכים\visual studio 2013\projects\draw\draw\bin\debug\8.png c:\users\itapi\onedrive\מסמכים\visual studio 2013\projects\draw\draw\bin\debug\9.png

for reason can't proper alphabetically order sorted list tried using linq sort() method,with no success.

looking solution.

thanks.

reason is string ordering rules string sorting. want take number part of , order it:

directory.getfiles(directory.getcurrentdirectory(), "*.png")          .orderby(f => int.parse(f.split('.')[0]));  

if not using split can use scott's suggestion use getfilenamewithoutextension:

directory.getfiles(directory.getcurrentdirectory(), "*.png")          .orderby(f => int.parse(path.getfilenamewithoutextension(f)));  

keep in mind naive solution assumes file names (number).png. if isn't case can first check if number , parse


Comments