用户类排序 [英] User class sorting
问题描述
亲爱的所有
我想开发一个通用的类排序功能。
目前,当我需要排序用户类的集合时,例如:人,
展览,国家
我需要为每个用户类实现一个比较器类,例如:
PersonComparer, ExhibitionComparer,CountryComparer
是否有任何解决方案允许我使用相同的
比较器对不同的类进行排序并使用用户类的不同属性?
用于展览
我的想法如下:
void VisualComparer(Collection items,TypeOf class,string sortProperty)
当我调用这个函数时
VisualComparer(Persons,typeOf(Person)," name");
然后这个函数将在Person.Name属性上基本执行排序
谢谢
这应该通过实现来完成menting IComparer接口和
将它传递给集合的Sort方法。
如果你希望实现IComparer的类是通用的(例如能够比较基于属性名称的任何类别的b $ b你应该实现它
如下:
class Comparer< T: IComparer< T>
{
私人PropertyInfo属性;
public Comparer(string propertyName)
{
PropertyInfo i = typeof(T).GetProperty(propertyName);
//要排序的属性必须具有可比性(字符串, ints和
其他原始数据类型是)
if(i.PropertyType.FindInterfaces(new TypeFilter(delegate(Type t,object
o)
{
返回t == typeof(IComparable);
}),null).Length == 0)
{
抛出新的ArgumentException(指定的属性不具有可比性);
}
this.property = i;
}
public int比较(T x,T y)
{
IComparable cx = property.GetValue(x,null)为IComparable;
IComparable cy = property.GetValue(y,null)为IComparable;
返回cx.CompareTo(cy);
}
}
//像这样使用
列表< Personl =新列表< Person>();
l.Add(new人(a));
l.Add(新人(c));
l.Add(new Person(" b" ));
l.Add(new Person(a));
l.Sort(new Comparer< Person>(" Name"));
希望这会有所帮助。
Wing Siu < wi ****** @ hotmail.comwrote in message
news:Ot ************** @ TK2MSFTNGP02.phx.gbl ...
亲爱的所有
我想开发一个通用的类排序功能。
目前,当我需要排序用户类的集合时,例如:
人,
展览,国家
我需要实现一个每个用户类的比较器类,例如:
PersonComparer,ExhibitionComparer,CountryComparer
是否允许我使用相同的类对不同类进行排序的任何解决方案
比较器并使用用户类的不同属性?
用于展览
我的想法如下:
void VisualComparer(集合项,TypeOf类,字符串sortProperty)
当我调用此函数时
VisualComparer(人,typeOf(Person),& ; name");
然后这个函数将基于Person.Na我执行分类的财产
谢谢
Hi Lebesgue >
感谢您的快速回复
Comparer的含义< T>
之前我没有看到这种格式,会你介意教我这是什么吗?
如果我有一个叫做人的ArrayList
我应该使用类跟随
person.Sort(new Comparer< Person>(" Name"));
谢谢
" Lebesgue" < le ****** @ gmail.com写信息
新闻:%2 *************** @ TK2MSFTNGP06.phx.gbl .. 。
这应该通过实现IComparer接口和
将其传递给集合的Sort方法来实现。
如果您希望实现IComparer的类是通用的(例如,能够根据属性名称比较任何类),您应该将其实现为
如下:
类Comparer< T:IComparer< T>
{
私人PropertyInfo属性;
public Comparer(string propertyName)
{
PropertyInfo i = typeof(T).GetProperty(propertyName);
//您要排序的属性必须具有可比性(字符串,整数
和
其他原始数据类型是)
if(i.PropertyType.FindInterfaces(new TypeFilter(delegate(Type t,
object
o)
{
return t == typeof(IComparable);
}),null).Length == 0)
{
抛出新的ArgumentException( 指定的财产不具有可比性;)
}
this.property = i;
}
public int比较(T x,T y)
{
IComparable cx = property.GetValue(x,null)为IComparable;
IComparable cy = property.GetValue(y,null)为IComparable;
返回cx.CompareTo(cy);
}
}
//像这样使用
列表< Personl =新列表< Person>();
l.Add(new Person(" a"));
l.Add(new Person(" c"));
l .Add(new Person(" b"));
l.Add(new Person(" a"));
l.Sort(new Comparer<人>("姓名")); <无线电通信/>
希望这会有所帮助。
Wing Siu < wi ****** @ hotmail.comwrote in message
news:Ot ************** @ TK2MSFTNGP02.phx.gbl ...
亲爱的所有
我想开发一个通用的类排序功能。
目前,当我需要排序用户类的集合时,例如:
人,
展览,国家
我需要实现一个每个用户类的比较器类,例如:
PersonComparer,ExhibitionComparer,CountryComparer
是否允许我使用$ b对不同类进行排序的任何解决方案$ b
相同
comparer并使用不同的属性用户类?
用于展览
我的想法如下:
void VisualComparer(Collection items,TypeOf class,string sortProperty )
当我打电话给这个功能时离子
VisualComparer(人物,typeOf(人物),名称);
然后这个函数将基本在Person.Name属性上执行排序
谢谢
< Tin Comparer的含义class是它的泛型类型参数。
泛型是.NET 2.0的新手。
你可以在这里了解它们:
http://msdn2.microsoft.com/en-us/library/512aeb7t。 aspx
如果您不使用.NET 2.0,可以修改代码以使用System.Type
作为参数而不是使用泛型。
如果你能处理它或者我应该为你修改代码,请告诉我。
Wing Siu < wi ****** @ hotmail.comwrote in message
news:em ************** @ TK2MSFTNGP03.phx.gbl ...
你好Lebesgue
谢谢你的快速回复
Comparer< T>是什么意思;
之前我没有看到这种格式,你介意教我这是什么吗?
如果我有一个叫做人的ArrayList
我应该使用类跟随
persons.Sort(new Comparer< Person>(" Name"));
谢谢
" Lebesgue" < le ****** @ gmail.com写信息
新闻:%2 *************** @ TK2MSFTNGP06.phx.gbl .. 。
>这应该通过实现IComparer接口并将其传递给集合的Sort方法来实现。
如果你想要的话实现IComparer是通用的类(例如能够比较任何基于属性名称的类)你应该如下所示实现它:
类Comparer< T:IComparer< T> ;
私有PropertyInfo属性;
public Comparer(string propertyName)
{PropertyInfo i = typeof(T).GetProperty(propertyName);
//要排序的属性必须具有可比性(字符串,整数
和
>其他原始数据类型是)
if(i.PropertyType.FindInterfaces(new TypeFilter(delegate(Type t,
) object
> o)
{
返回t == typeof(IComparable);
}),null).Length == 0)
{
抛出新的ArgumentException(指定的属性不具有可比性);
}
this.property = i;
}
公开int比较(T x,T y)
{IComparable cx = property.GetValue(x,null)为IComparable;
IComparable cy = property.GetValue(y,null)为IComparable;
返回cx.CompareTo(cy);
}
//像这样使用
List< Personl = new List< ;人>();
l.Add(新人(a));
l.Add(新人(c));
l.Add(新人(b));
l.Add(新人(a));
l.Sort(新的Comparer< Person>(" Name"));
希望这会有所帮助。
Wing Siu < wi ****** @ hotmail.comwrote in message
新闻:Ot ************** @ TK2MSFTNGP02.phx.gbl ...
< blockquote class =post_quotes>
亲爱的所有
我想开发一个通用的类排序功能。
目前,当我需要排序时用户类的集合,例如:
人,
展览,国家
我需要为每个实现一个比较器类用户类,例如:
PersonComparer,ExhibitionComparer,CountryComparer
是否允许我使用
对不同类进行排序的任何解决方案
相同
comparer并使用用户类的不同属性?
用于展览
我的想法如下:
void VisualComparer(Collection items,TypeOf class,string
sortProperty)
当我调用此函数时
VisualComparer(Persons,typeOf(Person)," name");
然后这个函数将在Person.Name属性上基本执行
排序
谢谢
Dear All
I would like to develop a generic class sorting function.
Currently, when I need sort a collection of user class, for example: Person,
Exhibition, Country
I need implement a comparer class for each user class, for example:
PersonComparer, ExhibitionComparer, CountryComparer
Is that any solution that allow me sort different class by using the same
comparer and use different attribute of the user class?
for exhibition
For my idea something like follow
void VisualComparer(Collection items, TypeOf class, string sortProperty)
When I call this function
VisualComparer(Persons, typeOf(Person), "name");
Then this function will basic on Person.Name property to perform sorting
Thanks
This should be accomplished by implementing an IComparer interface and
passing it to the Sort method of your collections.
If you want the class implementing IComparer to be generic (e.g. able to
compare any class based on property name) you should implement it as
follows:
class Comparer<T: IComparer<T>
{
private PropertyInfo property;
public Comparer(string propertyName)
{
PropertyInfo i = typeof(T).GetProperty(propertyName);
//the property you want to sort by must be comparable (strings, ints and
other primitive datatypes are)
if (i.PropertyType.FindInterfaces(new TypeFilter(delegate(Type t, object
o)
{
return t == typeof(IComparable);
}), null).Length == 0)
{
throw new ArgumentException("Specified property is not comparable");
}
this.property = i;
}
public int Compare(T x, T y)
{
IComparable cx = property.GetValue(x, null) as IComparable;
IComparable cy = property.GetValue(y, null) as IComparable;
return cx.CompareTo(cy);
}
}
//use it like this
List<Personl = new List<Person>();
l.Add(new Person("a"));
l.Add(new Person("c"));
l.Add(new Person("b"));
l.Add(new Person("a"));
l.Sort(new Comparer<Person>("Name"));
Hope this helps.
"Wing Siu" <wi******@hotmail.comwrote in message
news:Ot**************@TK2MSFTNGP02.phx.gbl...Dear All
I would like to develop a generic class sorting function.
Currently, when I need sort a collection of user class, for example:
Person,
Exhibition, Country
I need implement a comparer class for each user class, for example:
PersonComparer, ExhibitionComparer, CountryComparer
Is that any solution that allow me sort different class by using the same
comparer and use different attribute of the user class?
for exhibition
For my idea something like follow
void VisualComparer(Collection items, TypeOf class, string sortProperty)
When I call this function
VisualComparer(Persons, typeOf(Person), "name");
Then this function will basic on Person.Name property to perform sorting
Thanks
Hi Lebesgue
Thanks your quick reply
What''s meaning of Comparer<T>
I haven''t see this format before, would you mind teach me what it is?
If I have an ArrayList called persons
should I use the class like follow
persons.Sort(new Comparer<Person>("Name"));
Thanks
"Lebesgue" <le******@gmail.comwrote in message
news:%2***************@TK2MSFTNGP06.phx.gbl...This should be accomplished by implementing an IComparer interface and
passing it to the Sort method of your collections.
If you want the class implementing IComparer to be generic (e.g. able to
compare any class based on property name) you should implement it as
follows:
class Comparer<T: IComparer<T>
{
private PropertyInfo property;
public Comparer(string propertyName)
{
PropertyInfo i = typeof(T).GetProperty(propertyName);
//the property you want to sort by must be comparable (strings, intsand
other primitive datatypes are)
if (i.PropertyType.FindInterfaces(new TypeFilter(delegate(Type t,object
o)
{
return t == typeof(IComparable);
}), null).Length == 0)
{
throw new ArgumentException("Specified property is not comparable");
}
this.property = i;
}
public int Compare(T x, T y)
{
IComparable cx = property.GetValue(x, null) as IComparable;
IComparable cy = property.GetValue(y, null) as IComparable;
return cx.CompareTo(cy);
}
}
//use it like this
List<Personl = new List<Person>();
l.Add(new Person("a"));
l.Add(new Person("c"));
l.Add(new Person("b"));
l.Add(new Person("a"));
l.Sort(new Comparer<Person>("Name"));
Hope this helps.
"Wing Siu" <wi******@hotmail.comwrote in message
news:Ot**************@TK2MSFTNGP02.phx.gbl...Dear All
I would like to develop a generic class sorting function.
Currently, when I need sort a collection of user class, for example:
Person,
Exhibition, Country
I need implement a comparer class for each user class, for example:
PersonComparer, ExhibitionComparer, CountryComparer
Is that any solution that allow me sort different class by using the
same
comparer and use different attribute of the user class?
for exhibition
For my idea something like follow
void VisualComparer(Collection items, TypeOf class, string sortProperty)
When I call this function
VisualComparer(Persons, typeOf(Person), "name");
Then this function will basic on Person.Name property to perform sorting
Thanks
The meaning of <Tin Comparer class is that it''s a generic type parameter.
Generics are new to .NET 2.0.
You can learn about them here:
http://msdn2.microsoft.com/en-us/library/512aeb7t.aspx
If you are not using .NET 2.0, the code can be modified to use System.Type
as parameter instead of using generics.
Let me know if you can handle it or should I modify the code for you.
"Wing Siu" <wi******@hotmail.comwrote in message
news:em**************@TK2MSFTNGP03.phx.gbl...Hi Lebesgue
Thanks your quick reply
What''s meaning of Comparer<T>
I haven''t see this format before, would you mind teach me what it is?
If I have an ArrayList called persons
should I use the class like follow
persons.Sort(new Comparer<Person>("Name"));
Thanks
"Lebesgue" <le******@gmail.comwrote in message
news:%2***************@TK2MSFTNGP06.phx.gbl...>This should be accomplished by implementing an IComparer interface and
passing it to the Sort method of your collections.
If you want the class implementing IComparer to be generic (e.g. able to
compare any class based on property name) you should implement it as
follows:
class Comparer<T: IComparer<T>
{
private PropertyInfo property;
public Comparer(string propertyName)
{
PropertyInfo i = typeof(T).GetProperty(propertyName);
//the property you want to sort by must be comparable (strings, intsand
>other primitive datatypes are)
if (i.PropertyType.FindInterfaces(new TypeFilter(delegate(Type t,object
>o)
{
return t == typeof(IComparable);
}), null).Length == 0)
{
throw new ArgumentException("Specified property is not comparable");
}
this.property = i;
}
public int Compare(T x, T y)
{
IComparable cx = property.GetValue(x, null) as IComparable;
IComparable cy = property.GetValue(y, null) as IComparable;
return cx.CompareTo(cy);
}
}
//use it like this
List<Personl = new List<Person>();
l.Add(new Person("a"));
l.Add(new Person("c"));
l.Add(new Person("b"));
l.Add(new Person("a"));
l.Sort(new Comparer<Person>("Name"));
Hope this helps.
"Wing Siu" <wi******@hotmail.comwrote in message
news:Ot**************@TK2MSFTNGP02.phx.gbl...Dear All
I would like to develop a generic class sorting function.
Currently, when I need sort a collection of user class, for example:
Person,
Exhibition, Country
I need implement a comparer class for each user class, for example:
PersonComparer, ExhibitionComparer, CountryComparer
Is that any solution that allow me sort different class by using the
same
comparer and use different attribute of the user class?
for exhibition
For my idea something like follow
void VisualComparer(Collection items, TypeOf class, string
sortProperty)
When I call this function
VisualComparer(Persons, typeOf(Person), "name");
Then this function will basic on Person.Name property to perform
sorting
Thanks
这篇关于用户类排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!