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?

Recommended Answers

All 5 Replies

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

I'm sorting it inside the listview

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?

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;
            }
        }

    }
}

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.