it is not really possible to sort that way using strings, it will alsways put 1 before 2, if it was 02 then it would sort it, you could try splitting the string into a keyvalue pair and convert the numbers to actual numbers, but that would also break because of 22b :)
public class CustomComparer : IComparer<string>
{
public int Compare(string x, string y)
{
var regex = new Regex("(\\d+)");
// run the regex on both strings
var xRegexResult = regex.Match(x);
var yRegexResult = regex.Match(y);
// check if they are both numbers
if (xRegexResult.Success && yRegexResult.Success)
{
return int.Parse(xRegexResult.Groups[1].Value).CompareTo(int.Parse(yRegexResult.Groups[1].Value));
}
// otherwise return as string comparison
return x.CompareTo(y);
}
}
You would then use it like
var myComparer = new CustomComparer();
myListOfStrings.Sort(myComparer);
Should I place the class in App_Code? I have done that now and if I use your example I get:
cannot convert from 'holmemollevej.App_Code.CustomComparer' to 'System.Collections.Generic.IComparer<Umbraco.Core.Models.PublishedContent.IPublishedContent>'
Create a new CustomNameComparer.cs file, you should be able to create it anywhere in your project.
public class CustomNameComparer : IComparer<IPublishedContent>
{
public int Compare(IPublishedContent x, IPublishedContent y)
{
var regex = new Regex("(\\d+)");
// run the regex on both strings
var xRegexResult = regex.Match(x.Name);
var yRegexResult = regex.Match(y.Name);
// check if they are both numbers
if (xRegexResult.Success && yRegexResult.Success)
{
return int.Parse(xRegexResult.Groups[1].Value).CompareTo(int.Parse(yRegexResult.Groups[1].Value));
}
// otherwise return as string comparison
return x.Name.CompareTo(y.Name);
}
}
Sorting string with number in Umbraco razor
In a Umbraco 8 site I have an address in a string, so that string will contain a number, like: Street Road 2, Street Road 14, Street Road 22b etc.
I am sorting the list by address (among other values)
The expected result should be
But the actual result is
I know it is because of alphanumeric, but I can't figure out how to sort it correctly. Has anyone had a similar issue?
it is not really possible to sort that way using strings, it will alsways put 1 before 2, if it was 02 then it would sort it, you could try splitting the string into a keyvalue pair and convert the numbers to actual numbers, but that would also break because of 22b :)
This is nothing to do with Umbraco, it is just the way string sorting works
You would need to implement a custom IComparer
You would then use it like
just test it and you acually need to do 2 sorts otherwise things like 22d and 22a will not sort correctly, so you need to use the comparer like below
Thanks! I really appreciate your help!
Forgive my stupidity! Can you give an example of how to implement this in my use case:
Should I place the class in App_Code? I have done that now and if I use your example I get:
I'm guessing that is because apartments is a List of IPublishedContent.
I'll have a play and see if I can come up with something for you
Heroes isn't always wearing capes!
No guarentees, but try this
Create a new CustomNameComparer.cs file, you should be able to create it anywhere in your project.
where you are doing
Change it to
I had to move the order by apartmentItemStatus to a new var with the list after sorting, but other than that it works perfectly!
Thanks for the help! Really appreciate it!
Happy to help.
is working on a reply...