开发者

Sort varchar column starting with the first letter instead of the first character?

Using LINQ or SQL, how could I have the following strings, normally开发者_运维技巧 sorted as:

"banana"
apple
coconut

Sort as:

apple
"banana"
coconut


Updated based on your comment

IList<string> sorted = context.Terms.ToList()
    .OrderBy(t => Regex.Replace(t.Term, @"\W*","")).ToList();


On SQL you can sort it without needing REGEX functions per se, you can use PATINDEX. Try this:

SELECT *
FROM Table
ORDER BY RIGHT(Column,LEN(Column)-patindex('%[a-zA-Z]%',Column)+1)

This way you are sorting the Table using the first letter of the column, ignoring the others characters


You could add a column which contains just the alphanumeric string, then sort on this.

The function RemoveNonAlphaCharacters found here will allow you to filter out non alphanumeric characters. If the table is very small and performance is not a problem, you could simply

ORDER BY RemoveNonAlphaCharacters(columnToClean)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜