Good day 4 u all
美好的一天4你们
I have a list of objects
我有一个对象列表
My objects like
我的对象喜欢
Product = "iPhone";
Category = "SmartPhone";
Product = "HP";
Category = "PC";
Product = "HTC";
Category = "SmartPhone";
And I insert each object into my test so its like
我将每个对象插入到我的测试中,就像它一样
List<Myobject> MyList = new List<Myobject>();
And now I need to sord/order MyList by the category
现在我需要根据类别对MyList进行sord / order
As I need my list to show the SmartPhone category first then other
因为我需要我的列表首先显示SmartPhone类别然后是其他类别
3 个解决方案
#1
18
You can use List.Sort
您可以使用List.Sort
l.Sort((p, q) => p.Category.CompareTo(q.Category));
The advantage over the LINQ OrderBy
is that you'll order the list in-place instead of generating an IOrderedEnumerable<T>
that then you have to re-transform in a List<T>
.
LINQ OrderBy的优势在于您可以就地排序列表,而不是生成IOrderedEnumerable
#2
13
Check out the LINQ OrderBy extension method.
查看LINQ OrderBy扩展方法。
MyList.OrderBy (p => p.Category);
If you need a more complex way to sort the categories, you could create a class which implements the IComparer interface, and implement your sort logic in it.
如果需要更复杂的方法对类别进行排序,可以创建一个实现IComparer接口的类,并在其中实现排序逻辑。
public class SmartphonesFirst : IComparer<Product>
{
const string Smartphone = "Smartphone";
public int Compare( Product x, Product y )
{
if( x.Category == Smartphone && y.Category != Smartphone )
{
return -1;
}
if( y.Category == Smartphone && x.Category != Smartphone )
{
return 1;
}
else
{
return Comparer<String>.Default.Compare (x.Category, y.Category);
}
}
}
You can do it without using LINQ:
你可以不使用LINQ来做到这一点:
var l = new List<Product> ();
l.Add (new Product ()
{
Name = "Omnia 7",
Category = "Smartphone"
});
l.Add (new Product ()
{
Name = "Mercedes",
Category = "Car"
});
l.Add (new Product ()
{
Name = "HTC",
Category = "Smartphone"
});
l.Add (new Product ()
{
Name = "AMD",
Category = "CPU"
});
l.Sort (new SmartphonesFirst ());
foreach( var p in l )
{
Console.WriteLine (String.Format ("{0} : {1}", p.Category, p.Name));
}
Or, with using LINQ:
或者,使用LINQ:
var l = new List<Product> ();
l.Add (new Product ()
{
Name = "Omnia 7",
Category = "Smartphone"
});
l.Add (new Product ()
{
Name = "Mercedes",
Category = "Car"
});
l.Add (new Product ()
{
Name = "HTC",
Category = "Smartphone"
});
l.Add (new Product ()
{
Name = "AMD",
Category = "CPU"
});
var sorted = l.OrderBy (p => p, new SmartphonesFirst ());
foreach ( var p in sorted )
{
Console.WriteLine (String.Format ("{0} : {1}", p.Category, p.Name));
}
#3
6
You can use the Sort
method and a custom comparison, to sort by category (descending) and then by product (ascending):
您可以使用Sort方法和自定义比较,按类别(降序)排序,然后按产品(升序)排序:
MyList.Sort((a, b) => {
// compare b to a to get descending order
int result = b.Category.CompareTo(a.Category);
if (result == 0) {
// if categories are the same, sort by product
result = a.Product.CompareTo(b.Product);
}
return result;
});
If you want to single out smartphones, and then sort ascending:
如果你想挑出智能手机,然后按升序排序:
MyList.Sort((a, b) => {
int result = (a.Category == "SmartPhone" ? 0 : 1) - (b.Category == "SmartPhone" ? 0 : 1);
if (result == 0) {
result = a.Category.CompareTo(b.Category);
if (result == 0) {
result = a.Product.CompareTo(b.Product);
}
}
return result;
});
#1
18
You can use List.Sort
您可以使用List.Sort
l.Sort((p, q) => p.Category.CompareTo(q.Category));
The advantage over the LINQ OrderBy
is that you'll order the list in-place instead of generating an IOrderedEnumerable<T>
that then you have to re-transform in a List<T>
.
LINQ OrderBy的优势在于您可以就地排序列表,而不是生成IOrderedEnumerable
#2
13
Check out the LINQ OrderBy extension method.
查看LINQ OrderBy扩展方法。
MyList.OrderBy (p => p.Category);
If you need a more complex way to sort the categories, you could create a class which implements the IComparer interface, and implement your sort logic in it.
如果需要更复杂的方法对类别进行排序,可以创建一个实现IComparer接口的类,并在其中实现排序逻辑。
public class SmartphonesFirst : IComparer<Product>
{
const string Smartphone = "Smartphone";
public int Compare( Product x, Product y )
{
if( x.Category == Smartphone && y.Category != Smartphone )
{
return -1;
}
if( y.Category == Smartphone && x.Category != Smartphone )
{
return 1;
}
else
{
return Comparer<String>.Default.Compare (x.Category, y.Category);
}
}
}
You can do it without using LINQ:
你可以不使用LINQ来做到这一点:
var l = new List<Product> ();
l.Add (new Product ()
{
Name = "Omnia 7",
Category = "Smartphone"
});
l.Add (new Product ()
{
Name = "Mercedes",
Category = "Car"
});
l.Add (new Product ()
{
Name = "HTC",
Category = "Smartphone"
});
l.Add (new Product ()
{
Name = "AMD",
Category = "CPU"
});
l.Sort (new SmartphonesFirst ());
foreach( var p in l )
{
Console.WriteLine (String.Format ("{0} : {1}", p.Category, p.Name));
}
Or, with using LINQ:
或者,使用LINQ:
var l = new List<Product> ();
l.Add (new Product ()
{
Name = "Omnia 7",
Category = "Smartphone"
});
l.Add (new Product ()
{
Name = "Mercedes",
Category = "Car"
});
l.Add (new Product ()
{
Name = "HTC",
Category = "Smartphone"
});
l.Add (new Product ()
{
Name = "AMD",
Category = "CPU"
});
var sorted = l.OrderBy (p => p, new SmartphonesFirst ());
foreach ( var p in sorted )
{
Console.WriteLine (String.Format ("{0} : {1}", p.Category, p.Name));
}
#3
6
You can use the Sort
method and a custom comparison, to sort by category (descending) and then by product (ascending):
您可以使用Sort方法和自定义比较,按类别(降序)排序,然后按产品(升序)排序:
MyList.Sort((a, b) => {
// compare b to a to get descending order
int result = b.Category.CompareTo(a.Category);
if (result == 0) {
// if categories are the same, sort by product
result = a.Product.CompareTo(b.Product);
}
return result;
});
If you want to single out smartphones, and then sort ascending:
如果你想挑出智能手机,然后按升序排序:
MyList.Sort((a, b) => {
int result = (a.Category == "SmartPhone" ? 0 : 1) - (b.Category == "SmartPhone" ? 0 : 1);
if (result == 0) {
result = a.Category.CompareTo(b.Category);
if (result == 0) {
result = a.Product.CompareTo(b.Product);
}
}
return result;
});