I just made this sort method. It runs fine and the code looks okay in my eyes.
Is is possible to optimize it so it runs faster? If it's \$O(n^3)\$ now, it would be interesting changing it to \$O(n^2)\$ or something like that.
Side question: This is \$O(n^3)\$, right (I only like \$O(n^2)\$ and below)?
List<Log> errors = _logHandler.GetErrors(daysBack);
List<Log> errorsSorted = new List<Log>();
for (int i = 0; i < errors.Count; i++)
{
bool inserted = false;
for (int j = 0; j < errorsSorted.Count; j++)
{
if (errors[i].Date < errorsSorted[j].Date)
{
List<Log> tempList = new List<Log>();
tempList.AddRange(errorsSorted.GetRange(j,
errorsSorted.Count - j));
errorsSorted[j] = errors[i]; // replace at bigger date index
inserted = true;
errorsSorted.RemoveRange(j+1, errorsSorted.Count - (j+1));
// move all bigger dates one place to the right..
foreach (Log log in tempList)
{
errorsSorted.Add(log);
}
break;
}
}
if (!inserted)
{
errorsSorted.Add(errors[i]); // No date is bigger, add at the end
}
}
Log
Comparable and then use the inbuilt method. \$\endgroup\$