Write a C# program that inserts a new entered data into the array and deletes an existing data from the array. Insertion must be in ordered by name, if the names are the same, the record must be ordered according to surname.
After inserting "Ali Tarak" After Deleting "Ahmet Tas"
Name | Surname | Name | Surname | Name | Surname | ||
Ahmet | Tas | Ahmet | Tas | Ali | Kaya | ||
Ali | Kaya | Ali | Kaya | Ali | Tarak | ||
Ali | Yilmaz | Ali | Tarak | Ali | Yilmaz | ||
Derya | Birant | Ali | Yilmaz | Derya | Birant | ||
Zeynep | Ak | Derya | Birant | Zeynep | Ak | ||
Zeynep | Ak |

Satyapriya NayakPosted Dec 31, 2011, 1:17 AM
Try this...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
//class Program
//{
// static void Main(string[] args)
// {
// }
//}
public delegate int Comparer(object obj1, object obj2);
public class Name
{
public string FirstName = null;
public string LastName = null;
public Name(string first, string last)
{
FirstName = first;
LastName = last;
}
// this is the delegate method handler
public static int CompareFirstNames(object name1, object name2)
{
string n1 = ((Name)name1).FirstName;
string n2 = ((Name)name2).FirstName;
if (String.Compare(n1, n2) > 0)
{
return 1;
}
else if (String.Compare(n1, n2) < 0)
{
return -1;
}
else
{
return 0;
}
}
public override string ToString()
{
return FirstName + " " + LastName;
}
}
class SimpleDelegate
{
Name[] names = new Name[5];
public SimpleDelegate()
{
names[0] = new Name("Ali", "Kaya");
names[1] = new Name("Ali", "Tarak");
names[2] = new Name("Ali", "Yilmaz");
names[3] = new Name("Derya", "Birant");
names[4] = new Name("Zeynep", "Ak");
}
static void Main(string[] args)
{
SimpleDelegate sd = new SimpleDelegate();
// this is the delegate instantiation
Comparer cmp = new Comparer(Name.CompareFirstNames);
Console.WriteLine("\nBefore Sort: \n");
sd.PrintNames();
// observe the delegate argument
sd.Sort(cmp);
Console.WriteLine("\nAfter Sort: \n");
sd.PrintNames();
}
// observe the delegate parameter
public void Sort(Comparer compare)
{
object temp;
for (int i = 0; i < names.Length; i++)
{
for (int j = i; j < names.Length; j++)
{
// using delegate "compare" just like
// a normal method
if (compare(names[i], names[j]) > 0)
{
temp = names[i];
names[i] = names[j];
names[j] = (Name)temp;
}
}
}
}
public void PrintNames()
{
Console.WriteLine("Names: \n");
foreach (Name name in names)
{
Console.WriteLine(name.ToString());
}
Console.ReadLine();
}
}
}
Thanks
If this post helps you mark it as answer
Sam HobbsPosted Dec 31, 2011, 9:32 PM
Sarah ThomsonPosted Dec 31, 2011, 5:32 AM