1

I have a two list one is original and other one is copy of original one

    List<Button> buttonList; // this is the original list
    List<Button> copyButtonList;// this is the copy of button list; this use to sort the list

I want to sort the copyButtonList according to my custom insertion sort where I written in separate class

I clone the original list to copy list following way(s) and sorted it

        copyButtonList = buttonList.ToList();

        String s = SortEngine.insertionSort(copyButtonList); 
        msgList.Items.Add(s);

I also try following ways

        copyButtonList = new List<Button>(buttonList);

and

foreach (var b in buttonList) {
            copyButtonList.Add(b);
        }

after that I tried to print the two list as follows

foreach(var b in buttonList){
            msgList.Items.Add(b.Text);
        }
        foreach(var b in copyButtonList){
            msgList.Items.Add(b.Text);
        }

in the above three situations both list are sorted :( I want just sort the copyButtonList only, Can anyone point out the my mistakes I done here ?

Updated : my insertion sort algorithm is below

 public static String insertionSort(List<Button> button)
    {
        String key;
        int i = 0;
        int j;
        String s = "";
        for (j = 1; j < button.Count; j++)
        {
            key = button.ElementAt(j).Text;
            i = j - 1;
            while (i >= 0 && int.Parse(button.ElementAt(i).Text) > int.Parse(key))
            {
                button.ElementAt(i + 1).Text = button.ElementAt(i).Text;

                i = i - 1;

            }
            button.ElementAt(i + 1).Text = key;
            if (i == -1)
            {

                s=(button.ElementAt(i + 1).Text + " is the starting Item, keep this in before " + button.ElementAt(i + 2).Text);

            }
            else if (i == j - 1)
            {
                s=(button.ElementAt(i + 1).Text + " is the last Item, keep this in after  " + button.ElementAt(i).Text);

            }
            else
            {
                s=(button.ElementAt(i + 1).Text + " is between " + button.ElementAt(i).Text + " and " + button.ElementAt(i + 2).Text);

            }

        }

        if (button.Count == 1)
        {
            s= ("This is the first Item");
        }
        return s;
    }
Selaru Sinbath
  • 641
  • 1
  • 5
  • 14

3 Answers3

0

Actually speising's question is not dumb. Since you are not changing the Button there is not need for deep cloning. Your problem is probably somewhere else. Here is an example

public class Customer
{
    public string Name { get; set; }

    public override string ToString()
    {
        return Name; 
    }
}

public class CustomerComparer : IComparer<Customer>
{
    public int Compare(Customer x, Customer y)
    {
        return x.Name.CompareTo(y.Name);
    }
}


void Print()
{

   List<Customer> l1 = new List<Customer>();
   l1.Add(new Customer() {  Name="aa"});
   l1.Add(new Customer() { Name = "cc" });
   l1.Add(new Customer() { Name = "bb" });

   List<Customer> l2 = new List<Customer>(l1);


   l2.Sort(new CustomerComparer());
   foreach (var item in l1)
        Console.WriteLine(item);


   Console.WriteLine();
   foreach (var item in l2)
        Console.WriteLine(item);

   Console.ReadLine();

}

This prints

aa

cc

bb

and then

aa

bb

cc

Update

Your ARE changing the button so you need a deep copy.Use this to create the new list

using System.Linq;
copyButtonList = buttonList.Select(ee => new Button() {  Text= ee.Text}).ToList();

By "Changing the button" i mean lines like this

button.ElementAt(i + 1).Text = key;

You may have 2 lists but they both "point"/have the same objects.When you change the Text of a button in list 1 it means that the object is changed also in list 2.

You need to understand what is a value type and what is a reference type and their differences. Here are some links that will help you

http://www.albahari.com/valuevsreftypes.aspx

What is the difference between a reference type and value type in c#?

Also run the following console application on your pc and see what is printed

using System;

public struct Point
{
  public int X;
  public Point(int initialValue) { X = initialValue; }
}

class Program
{

  static void Main(string[] args)
  {

    Point point1 = new Point(5);

    Console.WriteLine("Before:" + point1.X);
    ChangePoint(point1);
    Console.WriteLine("After:" + point1.X);
    Console.ReadLine();
  }

  private static void ChangePoint(Point p)
  {
    p.X = 20;
  }
}

Then just change the word "struct" to "class" and see what is printed.

You get different result because structs are value types and classes are reference types.

Community
  • 1
  • 1
George Vovos
  • 7,563
  • 2
  • 22
  • 45
  • This is fine, but still I can not figure out the problem of my code – Selaru Sinbath Aug 29 '14 at 04:50
  • Your problem is that you are changing the buttons in insertionSort – George Vovos Aug 29 '14 at 06:35
  • Hey dear, can you explain the term that "your are changing the button" in your explanation, I want to copy one element at a time to other list and do some operation, after that add anther element to that list and do some operation like wise. I can't still understand the deep copy where here use – Selaru Sinbath Aug 31 '14 at 05:28
0

Your sorting algorithm modifies the Buttons, which are the same in both lists. if you really want to do it that way, you need to deep copy the objects, ie. make copies or each button, not just their references.

but a much better solution to sort the list would be to just sort the list, ie. switch the elements indices. (like button[i+1]=button[i])

ths
  • 2,858
  • 1
  • 16
  • 21
-1

You can use method mentioned here

List<YourType> oldList = new List<YourType>();
List<YourType> newList = new List<YourType>(oldList.Count);

oldList.ForEach((item)=>
    {
        newList.Add(new YourType(item));
    });
Community
  • 1
  • 1
Andrew
  • 7,619
  • 13
  • 63
  • 117
  • 4
    If OP is struggling with basic stuff, using `ForEach` and lambdas instead of `foreach` is probably a bad idea. – Athari Aug 28 '14 at 20:07