3

I have DataTable

Name  Date
bbb   01/01/2011
bbb   01/01/2012
aaa   01/01/2010
aaa   01/01/2011
bbb   01/01/2013
aaa   01/01/2012
bbb   01/01/2010
ccc   01/01/2010
aaa   01/01/2013
ccc   01/01/2012
ccc   01/01/2011

I need sort this table sort by name and every name by date:

Name  Date
aaa   01/01/2010
aaa   01/01/2011
aaa   01/01/2012
aaa   01/01/2013
bbb   01/01/2010
bbb   01/01/2011
bbb   01/01/2012
bbb   01/01/2013
ccc   01/01/2010
ccc   01/01/2011
ccc   01/01/2012

How to sort DataTable in c#?

I've tried the following:

   DataView dv = dt.DefaultView;
   dv.Sort = "col1 desc";
   DataTable sortedDT = dv.ToTable();

but this sort only by 1 column...

1

3 Answers 3

4

You can use LINQ to DataSet

var sortedDT = dt.AsEnumerable()
                 .OrderBy(r => r.Field<string>("Name"))
                 .ThenBy(r => r.Field<DateTime>("Date"))
                 .CopyToDataTable();

Use CopyToDataTable method to create new DataTable from ordered rows.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, but coma would be much easier=)
@Bryuk agree, in this particular case, when you have DataView, string-based sorting looks more simple. If later you will require more complex sorting, then take a look on Sorting with DataView article
2

http://msdn.microsoft.com/en-us/library/system.data.dataview.sort.aspx

All you need to do is add a comma between the columns in the dv.Sort line.

To clarify

dv.Sort = "Name, Date";

4 Comments

OMG! Sorry... Do I need to delete this question? Or accept and forget?=)
@Bryuk: Is Date a DateTime or String column?
If it's string it doesn't work. Add these rows and look at the "suprising" sort: dt.Rows.Add("Test", "01/01/2011"); dt.Rows.Add("Test", "02/01/2010");
I have to agree with @TimSchmelter here. If it's a string column you are much safer going the linq-to-dataset route where you can parse it to a date before doing the sort. That said, if the data you are currently dealing with happens to work with a simple string sort (i.e. you only ever have January 1st and are effectively sorting on year) then by all means take the easy solution. But please comment and unit test liberally.
2

You can use Linq-To-DataSet:

var orderedRows = from row in dt.AsEnumerable()
                  orderby row.Field<string>("Name"), row.Field<DateTime>("Date")
                  select row; 
DataTable tblOrdered = orderedRows.CopyToDataTable();

If the Date column is actually a string column you need to parse it to DateTime first.

Assuming valid formats:

var orderedRows = from row in dt.AsEnumerable()
                  let date = DateTime.Parse(row.Field<string>("Date"), CultureInfo.InvariantCulture)
                  orderby row.Field<string>("Name"), date 
                  select row;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.