file - Sorting the result of Directory.GetFiles in vb.net -
i have 1 directory contain tif formate file ,near 30 file name b_1 ,b_2...upto b_15 , f_1 ,f_2...upto f_1. when getting file getfile method.
dim di new io.directoryinfo("c:\abc\") dim diar1 io.fileinfo() = di.getfiles()
but while retriving using each loop getting result b_1,b_10,b_11,b_12,b_13,b_14,b_15,b_2,b_3...upto b_9 same f_1,f_10,f_11,f_12,f_13,f_14,f_15,f_2,f_3...upto f_9
but problem want in pattern b_1,b_2,b_3,b_4.....b_9,b_10,b_11......b_15 , f_1,f_2,f_3,f_4.....f_9,f_10,f_11......f_15
actually task getting file directory , join tiff file file f_1,b_1,f_2,b_2...f_9,b_9,f_10,b_10,f_11,b_11,....f_15,b_15
i have achieved everthing means join tiff , file start b , f storing in respective arraylist due comming file in b_,b_10..so on thats why getting problem...
plz me...
the simplest solution create method returns sort key string, instance, in situation, should suffice:
public function getfileinfosortkey(fi fileinfo) string dim parts() string = fi.name.split("_"c) dim sortkey string = nothing if parts.length = 2 sortkey = parts(1).padleft(10) & parts(0) else sortkey = fi.name end if return sortkey end function
then, can use method sort array of fileinfo
objects, this:
array.sort(diar1, function(x, y) getfileinfosortkey(x).compareto(getfileinfosortkey(y)))
if don't care keeping array, may want use orderby
extension method provided linq:
dim diar1 ienumerable(of fileinfo) = di.getfiles().orderby(of string)(addressof getfileinfosortkey)
alternatively, if using older version of visual studio not support lambda expressions, can creating separate comparer method, this:
public function fileinfocomparer(x fileinfo, y fileinfo) integer return getfileinfosortkey(x).compareto(getfileinfosortkey(y)) end function
then call array.sort
using comparer method, this:
array.sort(diar1, addressof fileinfocomparer)
Comments
Post a Comment