# VBForums CodeBank > CodeBank - ASP / ASP.NET / MVC / Web API >  Sorting a datatable for REALS!

## silentthread

Sorting a datatable for REALS! Just like they said in the South Park show where Kenny goes to heaven with his playstation system. "This is for reals."


Okay, folks here it is...

I'm a fan of Response.Write, and the other day, I needed to response.write the contents of a datatable. Don't ask me why. Just know that I had alot of neat javascript and ajax goodies going on. So, it was easier for me to just response.write the whole thing. Then I found out that...


misleading...  :-( Code:
mytable.DefaultView.Sort = "column asc"

Only works when you bind it, example a datagrid. This is a terrible thing. I can't believe, I was mislead to believe that DefaultView.Sort would do the trick. Shame on you world. lol.  :-)

Here is a function I created that does real sorting on a datatable....




good stuff man... Code:
Public Function SortTable(ByRef mytable As DataTable, ByVal column As String)
        'Sorting a table for reals! By SiLentThReaD, AKA Numbchucks.
         'create a blank table. tablecopy is the name
        Dim tablecopy As New DataTable
        'make an exact copy of the table you passed in by reference, and
        'store it in your tablecopy table
        tablecopy = mytable.Copy()
        'clear the contents of the table you just passed in, because
        'you will soon fill it with newly sorted rows
        mytable.Clear()
        'create a rows array
        Dim foundRows() As DataRow
        'assign the foundrows array the values returned
        'from the select. In this case, I left the
        'filterexpression blank, because I want to get
        'everything. I all I need to change is the sort 
        'order.
        foundRows = tablecopy.Select("", column + " asc")
         'iterate through each row and import them into
        'the table that you passed in by reference
        Dim dbrow As DataRow
        For Each dbrow In foundRows
            mytable.ImportRow(dbrow)
        Next
         'finally clear the contents of the tablecopy you created.
        tablecopy.Clear()
     End Function

----------


## jmcilhinney

This code is, I'm afraid, a waste of time.  You don't need to sort the DataTable itself.  The reason that sorting the DefaultView works when binding is that the data is taken from the DefaultView by the bound control.  If you want to see the data sorted then you just need to get the data from the DefaultView too.  Try this:
vb.net Code:
Dim table As New DataTable
 table.Columns.Add("Value", GetType(String))
 table.Rows.Add("C")
table.Rows.Add("B")
table.Rows.Add("D")
table.Rows.Add("A")
 For Each row As DataRow In table.Rows
    MessageBox.Show(CStr(row("Value")), "Rows Before Sort")
Next row
 For Each row As DataRowView In table.DefaultView
    MessageBox.Show(CStr(row("Value")), "DefaultView Before Sort")
Next row
 table.DefaultView.Sort = "Value ASC"
 For Each row As DataRow In table.Rows
    MessageBox.Show(CStr(row("Value")), "Rows After Sort")
Next row
 For Each row As DataRowView In table.DefaultView
    MessageBox.Show(CStr(row("Value")), "DefaultView After Sort")
Next row
I wrote this in a WinForms project so you can use something other than MessageBox.Show if you like, but it shows that after setting the DefaultView.Sort property the Rows property still returns the data in the original order but the DefaultView returns it in sorted order.  You just access the DataRowView objects from the DefaultView instead of the DataRow objects from the Rows collection.  If you really need a DataRow for some reason then you can get it from the Row property of the corresponding DataRowView.

----------


## silentthread

Dude, you are an animal. Thanks for sharing. I have given you reputation on this. Thanks again!

----------


## Longbow22

I am trying to sort a datatable before I write it out to a text file.
Fld4 has these values before sort:
0, 1, 5, 22, 31, 41, 53, 101, 119, 0, 163, 221, 0, 300, 312, 15

After the sort its exactly the same.
Any suggestions would be appreciated.
Thanks
LB


```
 Dim table As New DataTable

        ' Create four typed columns in the DataTable.
        table.Columns.Add("fld1", GetType(String)) ' String up to name variable
        table.Columns.Add("fld2", GetType(String)) ' field name
        table.Columns.Add("fld3", GetType(String)) ' string after name to start index
        table.Columns.Add("fld4", GetType(String)) ' start index
        table.Columns.Add("fld5", GetType(String)) ' string after start index to field length
        table.Columns.Add("fld6", GetType(String)) ' field length
        table.Columns.Add("fld7", GetType(String)) ' end of string

      For Each row As DataRow In table.Rows
            MessageBox.Show(CStr(row("fld4")), "Rows Before Sort")
        Next row

        table.DefaultView.Sort = "fld4 ASC"

        For Each row As DataRow In table.Rows
            MessageBox.Show(CStr(row("fld4")), "Rows After Sort")
        Next row
```

----------


## techgnome

Did you read the posts in this thread? It was mentioned:



> If you want to see the data sorted then you just need to get the data from the DefaultView too.


Which you're not doing in your code... 

there's even an example in the sample code:



```
For Each row As DataRowView In table.DefaultView
    MessageBox.Show(CStr(row("Value")), "DefaultView After Sort")
Next row
```

-tg

----------


## Longbow22

Yea, I read all the posts in this thread, I was just not sure how it applied to what I was trying to do.
Some of us are not experts like you, some of the obvious stuff that glares out at you does not always poke us in the eye.

Sorry to WASTE your time.

----------

