0

Hi
I have a column name date in my listview.Currently its been sorted by date,month,year but I want this column to be sorted by year ,month,date.How can I do it?

2
Contributors
5
Replies
9
Views
7 Years
Discussion Span
Last Post by sknake
0

Are you sorting it inside the listview or is it databound and are you sorting it when you bring the data forward?

0

And the column datatype is DateTime? It should either sort ascending or descending regardless of the date format I would think. Can you post your sort code?

0

Yes it is of type DateTime.The code below is in a class calledlistviewcolumnsorter.

private int ColumnToSort;   // Specifies the order in which to sort (i.e. 'Ascending').
        private SortOrder OrderOfSort;   // Case insensitive comparer object
        private CaseInsensitiveComparer ObjectCompare;   // Class constructor.  Initializes various elements

        public CListViewColumnSorter()
        {
            ColumnToSort = 0;
            OrderOfSort = SortOrder.None;
            ObjectCompare = new CaseInsensitiveComparer();
        }

        public int Compare(object x, object y)
        {
            int CompareResult;
            ListViewItem listviewX, listviewY;
            listviewX = (ListViewItem)x;
            listviewY = (ListViewItem)y;
            CompareResult = ObjectCompare.Compare(listviewX.SubItems[ColumnToSort].Text, listviewY.SubItems[ColumnToSort].Text);
            if (OrderOfSort == SortOrder.Ascending)
            {
                return CompareResult;
            }
            else if (OrderOfSort == SortOrder.Descending)
            {
                return (-CompareResult);
            }
            else
            {
                return 0;
            }
        }

        public int SortColumn
        {
            set
            {
                ColumnToSort = value;
            }
            get
            {
                return ColumnToSort;
            }
        }

        public SortOrder Order
        {
            set
            {
                OrderOfSort = value;
            }
            get
            {
                return OrderOfSort;
            }
        }

    }
}
0

Ah you are right -- DateTime's are stored as text in the gridview.

public int Compare(object x, object y) 
{
    int returnVal;
    // Determine whether the type being compared is a date type.
    try
    {
        // Parse the two objects passed as a parameter as a DateTime.
        System.DateTime firstDate = 
                DateTime.Parse(((ListViewItem)x).SubItems[col].Text);
        System.DateTime secondDate = 
                DateTime.Parse(((ListViewItem)y).SubItems[col].Text);
        // Compare the two dates.
        returnVal = DateTime.Compare(firstDate, secondDate);
    }
    // If neither compared object has a valid date format, compare
    // as a string.
    catch 
    {
        // Compare the two items as a string.
        returnVal = String.Compare(((ListViewItem)x).SubItems[col].Text,
                    ((ListViewItem)y).SubItems[col].Text);
    }
    // Determine whether the sort order is descending.
    if (order == SortOrder.Descending)
    // Invert the value returned by String.Compare.
        returnVal *= -1;
    return returnVal;
}

Taken from:
http://msdn.microsoft.com/en-us/library/ms996467.aspx

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.