"natural sorting linq" Code Answer's
You're definitely familiar with the best coding language C# that developers use to develop their projects and they get all their queries like "natural sorting linq" answered properly. Developers are finding an appropriate answer about natural sorting linq related to the C# coding language. By visiting this online portal developers get answers concerning C# codes question like natural sorting linq. Enter your desired code related query in the search bar and get every piece of information about C# code related question on natural sorting linq.
natural sorting linq
public class NaturalSortComparer<T> : IComparer<string>, IDisposable
{
private bool isAscending;
public NaturalSortComparer(bool inAscendingOrder = true)
{
this.isAscending = inAscendingOrder;
}
#region IComparer<string> Members
public int Compare(string x, string y)
{
throw new NotImplementedException();
}
#endregion
#region IComparer<string> Members
int IComparer<string>.Compare(string x, string y)
{
if (x == y)
return 0;
string[] x1, y1;
if (!table.TryGetValue(x, out x1))
{
x1 = Regex.Split(x.Replace(" ", ""), "([0-9]+)");
table.Add(x, x1);
}
if (!table.TryGetValue(y, out y1))
{
y1 = Regex.Split(y.Replace(" ", ""), "([0-9]+)");
table.Add(y, y1);
}
int returnVal = 0;
for (int i = 0; i < x1.Length && i < y1.Length; i++)
{
if (x1[i] != y1[i])
{
returnVal = PartCompare(x1[i], y1[i]);
return isAscending ? returnVal : -returnVal;
}
}
if (y1.Length > x1.Length)
{
returnVal = 1;
}
else if (x1.Length > y1.Length)
{
returnVal = -1;
}
else
{
returnVal = 0;
}
return isAscending ? returnVal : -returnVal;
}
private static int PartCompare(string left, string right)
{
int x, y;
if (!int.TryParse(left, out x))
return left.CompareTo(right);
if (!int.TryParse(right, out y))
return left.CompareTo(right);
return x.CompareTo(y);
}
#endregion
private Dictionary<string, string[]> table = new Dictionary<string, string[]>();
public void Dispose()
{
table.Clear();
table = null;
}
}
'Example: List<Photo> photos = DataManager.MainContext.Photos
' .Where(item => item.PhotoFilename != null)
' .AsEnumerable() !Important
' .OrderBy(item => item.PhotoFilename, new NaturalSortComparer<string>())
' .ToList();
Source: zootfroot.blogspot.com
All those coders who are working on the C# based application and are stuck on natural sorting linq can get a collection of related answers to their query. Programmers need to enter their query on natural sorting linq related to C# code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about natural sorting linq for the programmers working on C# code while coding their module. Coders are also allowed to rectify already present answers of natural sorting linq while working on the C# language code. Developers can add up suggestions if they deem fit any other answer relating to "natural sorting linq". Visit this developer's friendly online web community, CodeProZone, and get your queries like natural sorting linq resolved professionally and stay updated to the latest C# updates.