Using LINQ or SQL, how could I have the following strings, normally sorted as:
"banana"
apple
coconut
Sort as:
apple
"banana"
coconut
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)