There are a lot of examples out there to sort datagridview columns that are bound to a datasource, but I could only find one example that had a solution to solving the problem of sorting when the data is not bound to a datasource.
This is the data that I have, in a csv file
The third column (lets call it Order) contains data that is not integers, they are double values. I load it into a datagrid view by reading each line to a String Array, and then loading the array to the datagridview.
When I do a sortascending on the Order column, it sorts it as if it was String/ or Text instead of as a number. If the Order column had values like 10, 11, 7, 8, 172 it would sort it properly, but the decimal point screws it up into thinking that it is text. The solution to this problem is to use the SortCompare event, and to manually figure out if values are equal, greater than or less than. Initially I didn't understand the sortResult values. What does 1, -1, and 0 mean. And to be honest I still don't know, just trial and error. Some things to keep in mind. The Data must not be be bound, and the VirtualMode must be False, and the SortCompare is called when the Sort is called, in my case I call it programatically and have set it such. Also the code below assumes the data is numeric.
Private Sub DataGridView1_SortCompare(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewSortCompareEventArgs) Handles DataGridView1.SortCompare
If Double.Parse(e.CellValue1) > Double.Parse(e.CellValue2) Then
e.SortResult = 1
ElseIf Double.Parse(e.CellValue1) < Double.Parse(e.CellValue2) Then
e.SortResult = -1
e.SortResult = 0
e.Handled = True