Sorting Datatable column with special characters

Hi Guys,

Could you please provide a solution for my issue.

I have a datatable with me in excel sheet with 3 columns and I need to sort it based on column Incomplete.

I tried different approaches like

  1. Assign activity = (From x In statisticsDt .AsEnumerable()Order By convert.ToString(x("Incomplete“))Select x).CopyToDataTable()

  2. dt.DefaultView.Sort = (“[columnname] ASC”)
    dt = dt.DefaultView.ToTable

image

After using above solutions I got like this. which is not correct.

image

@ereddy068
Give a try to sort on numeric value. So you have rewrite the linq and removed the percent sign and Convert the remaining String to e.g. double

If I use below assign then I got output as below (From x In statisticsDt .AsEnumerable()
Order By convert.ToString(x("Incomplete“)) Ascending
Select x).CopyToDataTable()
Sorting is done perfectly but format is not correct for few values.
image

If I use Descending then I got output as below.

image
Is there anything I need to do for maintaining the same format for complete column

@ereddy068
Give a try on following

(From x In statisticsDt.AsEnumerable()
Order By convert.ToDouble(x(“Incomplete“).toString.replace(”%“,”").trim) Ascending
Select x).CopyToDataTable()

Kindly Note. Im travelling and do answer from my mobile. So it was Not possible to Check the Syntax. Maybe some corrections are to so. Maybe Convert does Not Like the comma. So do some Experiments in this direction

1 Like

Hi @ ereddy068,

it look like the problem is in your excel and not in your variable, before store the data in that column try to take the column or sheet wherever to Text or number … and run it again …

The problem is not always in what you are doing it can be in what you alredy have.

Hi,

I have got similar requirement to sort percentage in descending order and I have used the above code as mentioned and it found very helpful. But one issue is coming for 100%.it is coming at last while descending when it should be at the top. Can you provide the solution for this?I t would be helpful.I have used the below code.
(From x In statisticsDt .AsEnumerable()
Order By convert.ToString(x("Percentage“)) Descending
Select x).CopyToDataTable()

Thanks,
Nawaz.

@Mohammed_Nawazuddin
we woukld suggest that you will open an new topic for your case along with some sample data. So we can adress individually your case. Thanks