Babak-Aghili
جمعه 10 تیر 1384, 09:59 صبح
سلام.
همانطور که میدانیم متد سورت در ArrayList بصورت صعودی مرتب میکند ... حالا من خواستم بصورت نزولی تغییرش بدم
.... ولی کار نمیکنه :( .... روشم هم این بود که مقادیری که متد Compare برمیگرداند را در یک منفی ضرب کنیم که بجای صعودی ، نزولی مرتب کند ... .... کل برنامه هم همین دو تا کلاس است ... ( البته قانونا باید بصورت چهار تا کلاس باشه که صرفه جویی شد ! ) ... :confy2:
using System;
using System.Collections;
using System.Threading;
namespace Comparing
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public struct ZipCode: IComparable
{
public int zip;
string name;
public ZipCode(int _zip, string _name)
{
zip= _zip;
name= _name;
}
public int Zip
{
get { return zip; }
}
public string Name
{
get { return name; }
}
#region IComparable Members
public int CompareTo(object obj)
{
if(obj==null)
return 1;
if(obj.GetType() != this.GetType())
throw new ArgumentException("Comparing Apples and Oranges!");
ZipCode other= (ZipCode) obj;
int result= zip.CompareTo(other.zip);
if(result==0)
result= name.CompareTo(other.name);
return result;
}
#endregion
}
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//Thread.CurrentThread.CurrentCulture= new System.Globalization.CultureInfo("fa-IR");
ArrayList al= new ArrayList();
ZipCode zip= new ZipCode(121,"bbb");
al.Add(zip);
ZipCode zip1= new ZipCode(122,"ccc");
al.Add(zip1);
ZipCode zip2= new ZipCode(123,"hhh");
al.Add(zip2);
ZipCode zip3= new ZipCode(123,"fff");
al.Add(zip3);
ZipCode zip4= new ZipCode(125,"jjj");
al.Add(zip4);
//al.Sort(); // Default is Ascendigng sorting.
al.Sort(0,5, new Descending()); // I want to use Descending Sort.
foreach ( ZipCode z in al )
Console.WriteLine("Name: {0}, ZipCode: {1}", z.Name, z.Zip);
Console.ReadLine();
}
}
}
using System;
using System.Collections;
namespace Comparing
{
/// <summary>
/// Summary description for Descending.
/// </summary>
public class Descending: IComparer
{
public Descending()
{
}
#region IComarer Members
public int Compare(object x, object y)
{
if(x==null && y==null)
return 0;
else if(x==null)
return 1;
else if(y==null)
return -1;
if(x.GetType() != y.GetType())
throw new ArgumentException("Apples and Oranges!");
//IComparable comp= (IComparable) x;
IComparable comp= x as IComparable;
if(x==null)
throw new ArgumentException("Chi daadi tooo?");
int res= comp.CompareTo(y);
return res;
}
#endregion
}
}
:flower:
همانطور که میدانیم متد سورت در ArrayList بصورت صعودی مرتب میکند ... حالا من خواستم بصورت نزولی تغییرش بدم
.... ولی کار نمیکنه :( .... روشم هم این بود که مقادیری که متد Compare برمیگرداند را در یک منفی ضرب کنیم که بجای صعودی ، نزولی مرتب کند ... .... کل برنامه هم همین دو تا کلاس است ... ( البته قانونا باید بصورت چهار تا کلاس باشه که صرفه جویی شد ! ) ... :confy2:
using System;
using System.Collections;
using System.Threading;
namespace Comparing
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public struct ZipCode: IComparable
{
public int zip;
string name;
public ZipCode(int _zip, string _name)
{
zip= _zip;
name= _name;
}
public int Zip
{
get { return zip; }
}
public string Name
{
get { return name; }
}
#region IComparable Members
public int CompareTo(object obj)
{
if(obj==null)
return 1;
if(obj.GetType() != this.GetType())
throw new ArgumentException("Comparing Apples and Oranges!");
ZipCode other= (ZipCode) obj;
int result= zip.CompareTo(other.zip);
if(result==0)
result= name.CompareTo(other.name);
return result;
}
#endregion
}
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//Thread.CurrentThread.CurrentCulture= new System.Globalization.CultureInfo("fa-IR");
ArrayList al= new ArrayList();
ZipCode zip= new ZipCode(121,"bbb");
al.Add(zip);
ZipCode zip1= new ZipCode(122,"ccc");
al.Add(zip1);
ZipCode zip2= new ZipCode(123,"hhh");
al.Add(zip2);
ZipCode zip3= new ZipCode(123,"fff");
al.Add(zip3);
ZipCode zip4= new ZipCode(125,"jjj");
al.Add(zip4);
//al.Sort(); // Default is Ascendigng sorting.
al.Sort(0,5, new Descending()); // I want to use Descending Sort.
foreach ( ZipCode z in al )
Console.WriteLine("Name: {0}, ZipCode: {1}", z.Name, z.Zip);
Console.ReadLine();
}
}
}
using System;
using System.Collections;
namespace Comparing
{
/// <summary>
/// Summary description for Descending.
/// </summary>
public class Descending: IComparer
{
public Descending()
{
}
#region IComarer Members
public int Compare(object x, object y)
{
if(x==null && y==null)
return 0;
else if(x==null)
return 1;
else if(y==null)
return -1;
if(x.GetType() != y.GetType())
throw new ArgumentException("Apples and Oranges!");
//IComparable comp= (IComparable) x;
IComparable comp= x as IComparable;
if(x==null)
throw new ArgumentException("Chi daadi tooo?");
int res= comp.CompareTo(y);
return res;
}
#endregion
}
}
:flower: