c# - Sort list where specific character exists within string -



c# - Sort list where specific character exists within string -

here's hypothetical you. if have list of strings, possible rank list given character existing within string?

consider pseudo-code:

list<string> bunchofstrings = new list<string>; bunchofstrings.add("this should not @ top"); bunchofstrings.add("this should not @ top either"); bunchofstrings.add("this should not @ top"); bunchofstrings.add("this *should @ top"); bunchofstrings.add("this should not @ top"); bunchofstrings.add("this should *somewhere close top"); buncofstrings.orderby(x => x.contains("*"));

in above code, want re-order list whenever asterisk (*) appears within string, puts string @ top of list.

any ideas if possible linq or similar?

assuming want prioritise strings based on position of *, do

bunchofstrings.orderbydescending(x => x.indexof("*"))

use orderbydescending because strings don't contain * homecoming -1.

actually, looking farther it's not going work straight out box indexof. orderbydescending work going highest ranked index, in case going this should *somewhere close top rather this *should @ top because * has higher index in string.

so work need manipulate rankings little , utilize orderby instead

bunchofstrings.orderby(x => { var index = x.indexof("*"); homecoming index < 0 ? 9999 : index; });

note - 9999 aribtrary value can assume indexof never exceed

see live example

c# linq lambda custom-lists

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -