asp.net - Custom sort file name string array -


i'm retrieving string array of files , custom sort them substring in file name...using c# **.net 3.5. below working with.

         <%  string[] files = system.io.directory.getfiles("...path..." + pagename + "\\reference\\");            files = string.join(",", files).replace("...path...", "").replace("\\reference\\", "").replace(pagename, "").split(new char[] { ',' });                 foreach (string item in files)                 {                   response.write("<a href=" + pagename + "/reference/" + system.io.path.getfilename(item) + " target='_blank'>" + item.replace("_", "  ").replace(".pdf", " ") + "</a>");                 }        %>    

i'm c# noob, , don't know go here. basically, i'm looking substring in file name determine order (e.g., "index","reference","list"; file including string "index" listed first). perhaps there better way it. appreciated.

you can use linq order array filenames. in general, use path class if you're working paths.

string fullpath = path.combine(directory, pagename, "reference"); var filepaths = directory.enumeratefiles(fullpath, "*.*", searchoption.topdirectoryonly)     .select(fp => new{ fullpath = fp, filename=path.getfilename(fp) })     .orderbydescending(x => x.filename.indexof("index", stringcomparison.ordinalignorecase) >= 0)     .thenbydescending(x => x.filename.indexof("reference", stringcomparison.ordinalignorecase) >= 0)     .thenbydescending(x => x.filename.indexof("list", stringcomparison.ordinalignorecase) >= 0)     .thenby(x=> x.filename)     .select(x => x.fullpath); foreach(string filepath in filepaths)     ;// ... 

if don't want compare case-insensitively (so "index" , "index" considered same) use string.contains instead of string.indexof + stringcomparison.ordinalignorecase.


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -