SortedList Desc Order

前端 未结 4 702
闹比i
闹比i 2021-01-17 07:09

I am using SortedList to arrange arraylist records dynamically in sort order by datecolumn, but by default it is sorting in ascending

4条回答
  •  灰色年华
    2021-01-17 08:06

    There is no way to instruct the SortedList to do sorting in descended order. You have to provide your own Comparer like this

        class DescendedDateComparer : IComparer
        {
            public int Compare(DateTime x, DateTime y)
            {
                // use the default comparer to do the original comparison for datetimes
                int ascendingResult = Comparer.Default.Compare(x, y);
    
                // turn the result around
                return 0 - ascendingResult;
            }
        }
    
        static void Main(string[] args)
        {
            SortedList test = new SortedList(new DescendedDateComparer());
        }
    

提交回复
热议问题