c# - How to group a list of titles by the first letter with linguistic rules? -
i have list of game titles trying display. using orderby()
sorts list according title rules current culture, en-us list this:
- arthur's game
- the best game ever
- carl's game
- david's game
- earnest's game
- an even better game
that's great , want.
but how take list , group first letter (the letter in bold), in culture-sensitive way? more specifically, how actual string orderby()
used sort list way?
i tried looking @ system.globalization.sortkey.originalstring
returned same string given.
i know can take sloppy way out , trim "a", "an" , "the" start of string, hoping .net had way hook culture-sensitive sorting logic more robust solution.
edit: looks mistaken, , groupby()
not, in fact, sort way. messed sample list. looks have trim away "a", "an", , "the" prefixes. @stuartd pointing out error.
i don't know implemented solution sort titles required. since words exclude not many (as believe), define own list of exclusions like
list<string> exclusions = new list<string>() { "the", "an", "a", };
based on exclusions order titles (case insensitive in implementation) using simple query ignoring exlusions defined:
var orderedtitles = titles.orderby( t => string.join(" ", t.split(' ') .where(s => !exclusions.any(x => x.equals(s, stringcomparison.ordinalignorecase)))));
the idea split title spaces, , join without excluded words. after simple order job.
- a simple track1
- a simple track2
if need ignore exclusions until first non-excluded word, improve query using skip
.
var result = titles.orderby( t => string.join(" ", t.split(' ') .skipwhile(s => exclusions.any(x => x.equals(s, stringcomparison.ordinalignorecase)))));
- a simple track2
- a simple track1
Comments
Post a Comment