Maha

Maha

  • NA
  • 0
  • 325.9k

ArrayList

Jun 28 2013 7:20 AM
Please correct this program to give following output.

/*
Abby Normal, 25
Jane Doe, 76
John Doe, 84
*/

using System;
using System.Collections;

namespace vi1
{
class Program
{
static void Main(string[] args)
{
ArrayList people = new ArrayList(); //this requires using System.Collections;
people.Add(new Person("John", "Doe", 84));
people.Add(new Person("Abby", "Normal", 25));
people.Add(new Person("Jane", "Doe", 76));

people.Sort();

for(int x=0; x<4; x++)
Console.WriteLine(people.ToString());

Console.ReadLine();
}
}
}
internal class Person : IComparable
{
private string fName;
private string lName;
private int age;

public Person(string fName, string lName, int age)
{
this.fName = fName;
this.lName = lName;
this.age = age;
}
new public string ToString()
{
return String.Format("{0}, {1}, {2}", fName, lName, age);
}

public int CompareTo(object o)
{
int returnVal;

Person temp = (Person)o;

if(this.age>temp.age)
returnVal=1;
else
if(this.age<temp.age)
returnVal=-1;
else
returnVal=0;

return returnVal;
}
}


Answers (2)