c# - Sort a collection based on another collection -


i have collection of file names part of pathname being specific word. can order collection :

var files = f in checkedlistbox1.checkeditems.oftype<string>()             orderby f.substring(0,3)             select f; 

but now, want sort not alphabetical order on pathname part according specific order given collection.

so let's pathname part can "ate", "det" , "rti". have string collection : {"det", "ate", "rti"} want use sort filenames after sorting, filenames appear partname in order "det" first, "ate", "rti". how achieve -> need use own comparer ?

three different variants, depending if want use string[], list<string> or dictionary<string, int> (good if have many elements search for)

string[] collection = new[] { "det", "ate", "rti" }; var files = f in checkedlistbox1.checkeditems.oftype<string>()             orderby array.indexof(collection, f.substring(0, 3))             select f;  list<string> collection2 = new list<string> { "det", "ate", "rti" }; var files2 = f in checkedlistbox1.checkeditems.oftype<string>()             orderby collection2.indexof(f.substring(0, 3))             select f;  dictionary<string, int> collection3 = new dictionary<string, int>              { { "det", 1 }, { "ate", 2 }, { "rti", 3 } };  func<string, int> getindex = p => {     int res;     if (collection3.trygetvalue(p, out res))     {         return res;     }     return -1; };  var files3 = f in checkedlistbox1.checkeditems.oftype<string>()                 orderby getindex(f.substring(0, 3))                 select f; 

i'll add linq doesn't have "generic" indexof method, can build 1 written here how index using linq?


Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -