I have a project where I have a web store with items fetched from a list. Some items are named TERRAIN 1
, TERRAIN 2
and so on. While other items are named for example BOAT
.
The problem is that when I order the list alphabetically, TERRAIN 12
will show up before TERRAIN 2
.
Is there any way I can order the list the way that I want:
TERRAIN 1
TERRAIN 2
TERRAIN 12
and still be able to order the products that don't have numbers in an alphabetical order:
BOAT
CABIN
TRAILER
I have tried splitting the names and ordering them in this way:
`var ordered = DisplayProducts.Select(s => new { Str = s, Split = s.Product.ProductName.Split(' ') })
.OrderBy(x => x.Split[0])
.ThenBy(x => int.Parse(x.Split[1]))
.Select(x => x.Str)
.ToList();`
It works well with the products that have numbers. However that causes problems when there's no value in x.Split[1]
, because the index is out of range.