sparta93
sparta93

Reputation: 3854

How to rearrange the columns in an array in C#?

So I have an array with 4 columns. And I also have a separate list which defines each column with a string. Something like the snippet below:-

List<string> headers = new List<string>();
headers.Add("Name");
headers.Add("Number");
headers.Add("ID");
headers.Add("License");

The array looks like this

Max 32445 1 KPFG35
Bill 33234 2 DGWEF9
Ruth 89428 3 SFD3FG

... and so on.

Now, lets say someone wants that same array however with the columns arranged as ID, Name, Number, License. How can I manipulate the columns in the array and produce a new one to return? thank you for any help!

so in the case mentioned above, it would return

1 Max 32445 KPFG35
2 Bill 33234 DGWEF9
3 Ruth 89428 SFD3FG

Upvotes: 0

Views: 454

Answers (3)

hdz
hdz

Reputation: 305

As others have suggested: It would probably be better for you to store these as objects for maintainability.

But It sounds like you have a requirement that the array is what you want to keep. Since all you really want to do is move a column over you and produce a new list you could do something like:

private static IEnumerable<string> MoveIndexToFirst(List<string> input)
{
    for(int i = 0; i < input.Count; i+=4 )
    {
        yield return input[i+2];
        yield return input[i];
        yield return input[i+1];
        yield return input[i+3];
    }
}

usage:

List<string> newList = MoveIndexToFirst(yourData).ToList();

Upvotes: 0

progyammer
progyammer

Reputation: 1508

You are not actually shifting the columns but are rearranging them. Shifting means the columns remain in the same order but the row is rotated left or right. Anyway, doing it without using data structures but just using arrays would take the following form in code:

//Assuming the array is of type string
string[,] arr <--- the array we are talking about
string[,] ta = new string[3,4]; //a temporary array

/* The columns are arranged in the order: Name    Number    ID    License
    and we want it as: ------------       ID    Name    Number    License

    So, if the order of the columns is:    1   2   3   4, 
    we now want it as:  --------------     3   1   2   4  */

string order = "3124";
for(int i=0; i<3; i++){
    for(int j=0; j<4; j++){
        int k = int.Parse(order[j].ToString())-1;
                //k stores the column number in arr to be added to ta
        ta[i,j] = arr[i,k];
    }
}

//ta now stores arr in the new order
//you can either change the value of arr to the new array: arr = ta;
//or you can now make all your references to the new array ta

Hope it helps you.

Upvotes: 0

Berkay Yaylacı
Berkay Yaylacı

Reputation: 4513

I don't know if you have to use List or not. But here is a solution that may help you. I suggest you to use DataTable.

Basically I have create a form with datagridview and a button,

DataTable dt = new DataTable();

In form's load,

 private void Form1_Load(object sender, EventArgs e)
    {
        dt.Columns.Add("Name");
        dt.Columns.Add("Number");
        dt.Columns.Add("ID");
        dt.Columns.Add("License");
        string[] array = { "Max", "32445", "1", "KPFG35", "Bill", "33234", "2", "DGWEF9", "Ruth", "89428", "3", "SFD3FG" };

        for (int i = 0; i < array.Length + 1; i++)
        {

            if (i != 0 && i % 4 == 0)  // every 4th item should be split from list
            {
                string[] tempArray = new string[4]; //temp array will keep every item until 4th one.
                tempArray = array.Take(i).ToArray(); //Take until 4th item.
                array = array.Skip(i).ToArray(); //then we don't need that items so we can skip them
                dt.Rows.Add(tempArray); //Row is done.
                i = -1; //Every skip will generate a new array so it should go back to 0.

            }
        }
        dataGridView1.DataSource = dt;

    }

And there is a button to change order with SetOrdinal,

private void button1_Click(object sender, EventArgs e)
        {
            dt.Columns["ID"].SetOrdinal(0);
            dataGridView1.DataSource = null;
            dataGridView1.DataSource = dt;
            dataGridView1.Refresh();

        }

Output,

enter image description here

After button click ID column was at 0. (the second one)

Hope helps, (Not sure if you have to use List<string>, but it might be a clue for you.

Upvotes: 2

Related Questions