c# - OrderBy ignoring accented letters -
i want method orderby()
orders ignoring accented letters , @ them non-accented. tried override orderby()
seems can't because static method.
so want create custom lambda expression orderby()
, this:
public static iorderedenumerable<tsource> toorderby<tsource, tkey>( ienumerable<tsource> source, func<tsource, tkey> keyselector) { if(source == null) return null; var seenkeys = new hashset<tkey>(); var culture = new cultureinfo("pt-pt"); return source.orderby(element => seenkeys.add(keyselector(element)), stringcomparer.create(culture, false)); }
however, i'm getting error:
error 2 type arguments method 'system.linq.enumerable.orderby<tsource,tkey>(system.collections.generic.ienumerable<tsource>, system.func<tsource,tkey>, system.collections.generic.icomparer<tkey>)' cannot inferred usage. try specifying type arguments explicitly.
seems doesn't stringcomparer
. how can solve this?
note:
i tried use removediacritics()
here don't know how use method in case. tried this seems nice too.
orderby
takes keyselector
first argument. keyselector
should func<string,t>
. need method takes string , returns value enumeration should sorted.
unfortunatly i'm not sure how determine if character "accented letter". removediacritics
doesn't work é
.
so let's assume have method called isaccentedletter
determines if character accented letter:
public bool isaccentedletter(char c) { // i'm afraid not job return charunicodeinfo.getunicodecategory(c) == unicodecategory.nonspacingmark; }
so can sort list that:
string[] mystrings = getstrings(); // whereever strings come var ordered = mystrings.orderby(s => new string(s.select(c => isaccentedletter(c) ? ' ' : c).toarray()), stringcomparer.create(culture, false));
the lambda expression takes string , returns same string, replaced accented letters empty space.
orderby
sorts enumeration these strings, , "ignores" accented letters.
update: if have working method removediacritics(string s)
returns strings accented letters replaced want, may call orderby
this:
string[] mystrings = getstrings(); var ordered = mystrings.orderby(removediacritics, stringcomparer.create(culture, false));
Comments
Post a Comment