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            }        }
Optimizing error-sorting method
c#;performance;algorithm;sorting;reinventing the wheel
You are just sorting by date, correct? I'd useusing System.Linq;// ....List<Log> errors = _logHandler.GetErrors(daysBack);List<Log> errorsSorted = errors.OrderBy(l => l.Date).ToList();According to this stackoverflow question, OrderBy uses QuickSort in this case, which usually has a run time of O(n log n).