Sort Data Table not working

Hi,
I have a date column in a data table, which I’m trying to sort by oldest to newest. However, Sort data table activity doesn’t work.
Please advise.
Thank you

Can you show us how you configured the sort data table activity?

Hi @hp321

You can also try to sort using linq
dtInput.AsEnumerable().OrderBy(Function(row) Cdate(row(“DateColumn”))).CopyToDataTable()

Regards

Sure.

Here is a sample of the dates after running with the above activity:
image

Hi @hp321
Use the below code

(From r In DT Order By DateTime.ParseExact(r.Item(“YourColumnName”).ToString, “MM/dd/yyyy hh:mm:ss tt”, System.Globalization.CultureInfo.InvariantCulture) Descending Select r).CopyToDataTable

thanks, @rashmisingh2695, please see the error I’m getting:

usually I solve this by replacing copied “” with my own, something to do with language formatting, or perhaps you have some extra ones in your expression.

I find it best to type my expressions even though I’m using a manual, forum og AI assistance.

cheers

Thanks @lakshmi.mp ,
Getting the same error as above…

Hi @hp321 ,
Please try the following LINQ to sort your data.

DT.AsEnumerable.OrderBy(Function(r) dateTime.ParseExact(r(“DateColumn”).ToString, “M/dd/yyyy hh:mm:ss tt”, System.Globalization.CultureInfo.InvariantCulture)).CopyToDataTable

If you are doing a copy/paste of the code, please ensure your remove and rewrite all the quotes "
Please let us know if it works.

Thanks,
Anjani

Hi @hp321 Check the double quotes "" and replace with this

1 Like

It seems to be sorted from newest to oldest now. Try changing it from Descending to Ascending?

Just to double check, is the column specified as date or string? If it’s not of type date, try changing the type and try again.

thanks @OGunnarsson , can you please give an example of what you mean?

Please check the double quotes once in date column.

1 Like

To watch out for any extra quotations ( " " ) or replacing copied ones with your own by typing them into you expression

In this case, DT.AsEnumerable.OrderBy(Function(r) dateTime.ParseExact(r( → “DateColumn” ← ).ToString, → “M/dd/yyyy hh:mm:ss tt” <-, System.Globalization.CultureInfo.InvariantCulture).CopyToDataTable *note, ← → need to be removed :slight_smile:

Hi @hp321 ,

(From r In DT Order By DateTime.ParseExact(r.Item(“YourColumnName”).ToString, “MM/dd/yyyy hh:mm:ss tt”, System.Globalization.CultureInfo.InvariantCulture) Descending Select r).CopyToDataTable

Use above code and check the double quotes in (“YourColumnName”) and “MM/dd/yyyy hh:mm:ss tt” in date time format.
Please check and update…

image

dtSample = dtSample.AsEnumerable.OrderByDescending(Function(row) CDate(row("End Date"))).CopyToDataTable

image

Thanks all for your help!
It seems like I was copying the expressions rather than typing/retyping the quotation marks.

2 Likes

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.