Maha

Maha

  • NA
  • 0
  • 326k

Array.Sort()

Aug 29 2012 12:09 PM
In this program Array.Sort(x, new data.sortDes()); is used. Please give me relevant website address to get more information about this. Problem is highlighted.

using System;
using System.Collections;

namespace ConsoleApplication5
{
class data : IComparable
{
string name;
int age;

public data()
{
}
public data(string name, int age)
{
this.name = name;
this.age = age;
}
public string _name
{
get{return name;}
}
public int _age
{
get{return age;}
}
public int CompareTo(object obj)
{
data m = (data)obj;
if (m._age < this._age)
return 1;
else if (m._age > this._age)
return -1;
else
return 0;
}
public class sortDes : IComparer
{
int IComparer.Compare(object x, object y)
{
data m = (data)x;
data n = (data)y;

if (m._age < n._age)
return 1;
else
if (m._age > n._age)
return -1;
else
return 0;
}
}
}
class Program
{
static void Main(string[] args)
{
data[] x = new data[4];

x[0] = new data("Ali", 10);
x[1] = new data("Mohammed", 24);
x[2] = new data("Wael", 3);
x[3] = new data("Slah", 34);

// Array.Sort(x) //for will sort it Ascending
Array.Sort(x, new data.sortDes());

foreach (data d in x)
Console.WriteLine(d._name + " " + d._age.ToString());

Console.ReadKey();
}
}
}
/*
Slah 34
Mohammed 24
Ali 10
Wael 3
*/

Answers (2)