9

I have this very simple array which I want to be able to move around some items in. Are there any built in tools in c# to do this? If not, du you have any suggestion in how to do it.

In example

var smallArray = new string[4];
smallArray[0] = "a";
smallArray[1] = "b";
smallArray[2] = "c";
smallArray[3] = "d";

And lets say I want to (programmatically) swap index 2 and 0, creating

smallArray[0] = "c";
smallArray[1] = "a";
smallArray[2] = "b";
smallArray[3] = "d";

Thanks.

4
  • 1
    Just to be clear: by "shift", do you mean "swap"? Commented Aug 30, 2011 at 12:13
  • duplicate of stackoverflow.com/questions/552731/… Commented Aug 30, 2011 at 12:17
  • No, sorry my first example bas a bit fuzzy, I've made the example a bit more clear now. Commented Aug 30, 2011 at 12:19
  • Hatchet, no... Its note a dupe. Thats a SWAP and im into Shifting. Ready my post Commented Aug 30, 2011 at 12:20

4 Answers 4

18

EDIT: Okay, now you've changed the example, there's nothing built-in - and it would actually be a bit of a pain to write... you'd need to consider cases where you're moving it "up" and where you're moving it "down", for example. You'd want unit tests, but I think this should do it...

public void ShiftElement<T>(this T[] array, int oldIndex, int newIndex)
{
    // TODO: Argument validation
    if (oldIndex == newIndex)
    {
        return; // No-op
    }
    T tmp = array[oldIndex];
    if (newIndex < oldIndex) 
    {
        // Need to move part of the array "up" to make room
        Array.Copy(array, newIndex, array, newIndex + 1, oldIndex - newIndex);
    }
    else
    {
        // Need to move part of the array "down" to fill the gap
        Array.Copy(array, oldIndex + 1, array, oldIndex, newIndex - oldIndex);
    }
    array[newIndex] = tmp;
}

You should probably consider using a List<T> instead of an array, which allows you to insert and remove at particular indexes. Those two operations will be more expensive than only copying the relevant section, but it'll be a lot more readable.

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

1 Comment

Actually thats my bad, i didnt mean swap, I want to be able to move around the elements by setting a new index! (updated my example). Thanks!
6

It's an existing question:

C# Array Move Item (Not ArrayList/Generic List)

The answer is:

void MoveWithinArray(Array array, int source, int dest)
{
  Object temp = array.GetValue(source);
  Array.Copy(array, dest, array, dest + 1, source - dest);
  array.SetValue(temp, dest);
}

1 Comment

I looked at that one but failed to implement it.
-1

This solved my problem

var arr = new ArrayList 
    {
        "a", "b", "c", "d", "e", "f"
    };
var first = arr[2];  //pass  the value which you want move from 
var next = arr[5]; //pass the location where you want to place
var m = 0;
var k = arr.IndexOf(first, 0, arr.Count);
m = arr.IndexOf(next, 0, arr.Count);
if (k > m)
{
    arr.Insert(m, first);
    arr.RemoveAt(k + 1);
}
else
{
    arr.Insert(k, next);
    arr.RemoveAt(m + 1);
}

returns a, b, f, c, d, e

Comments

-1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SecondLargest
{
    class ArraySorting
    {
        public  static void Main()
        {
         int[] arr={5,0,2,1,0,44,0,9,1,0,23};

         int[] arr2 = new int[arr.Length];
         int arr2Index = 0;
         foreach (int item in arr)
         {
           if(item==0)
           {
               arr2[arr2Index] = item;
               arr2Index++;
           }
         }
         foreach (int item in arr)
         {
            if(item!=0)
            {
                arr2[arr2Index] = item;
                arr2Index++;
            }
         }

         foreach (int item in arr2)
         {
             Console.Write(item+" ");
         }

            Console.Read();

        }
    }
}

1 Comment

Welcome to Stack Overflow! While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.

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.