8

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.

Bouke Versteegh
  • 4,097
  • 1
  • 39
  • 35
Eric Herlitz
  • 25,354
  • 27
  • 113
  • 157

4 Answers4

17

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 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! – Eric Herlitz Aug 30 '11 at 12:18
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);
}
Community
  • 1
  • 1
balexandre
  • 73,608
  • 45
  • 233
  • 342
0
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();

        }
    }
}
Samadhan
  • 389
  • 1
  • 5
  • 18
  • 1
    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. – ryanyuyu Nov 03 '15 at 21:00
0

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

Eric Herlitz
  • 25,354
  • 27
  • 113
  • 157