How to sort Excel columns left‑to‑right based on numeric values in Row 1?

Hi everyone,

I need help sorting Excel columns left‑to‑right based on the numeric values in the first row.

Here is an example of my data:

What I need UiPath to do is: Sort the entire sheet left‑to‑right based on the numbers in Row 1

So that column order follows:
1, 2, 3, 6, 8, 9, 10, 11, 12…

What is the best way in UiPath to sort columns left‑to‑right using the numeric values in the first row using UiPath?

@fsgmacalino

I assume you are looking for rearranging the columns with data.
You can follow this approach, read the data into datatable, do the rearrangement and write it back to the excel file.

@fsgmacalino

  1. read data into datatable
  2. use assign with dt = dt.DefaultView.ToTable(false,dt.Columns.Cast(Of System.Data.DataColumn).OrderBy(function(x) If(String.IsNullOrEmpty(dt.Rows(0)(x.ColumnName).ToString),1000,Cint(dt.Rows(0)(x.ColumnName).ToString))).Select(function(x) x.ColumnName).ToArray)
  3. Write the data back to excel

cheers