基于某些条件的c#排序列表
我想以最有效的方式对条目列上给定列表中的条目进行排序。下面是我的i条目的外观示例基于某些条件的c#排序列表,c#,linq,C#,Linq,我想以最有效的方式对条目列上给定列表中的条目进行排序。下面是我的i条目的外观示例 #No Detail Entry Number Rate 1 Carpool at 5$ C 1 5 Carpool at 5$ H
#No Detail Entry Number Rate
1 Carpool at 5$ C 1 5
Carpool at 5$ H 2 5
2 Played Cricket at 2$ X 1 2
Played Cricket at 2$ O 2 2
3 Done something at 4$ "" 0 4
4 Done something else at 9$ M 1 9
5 Watched movie at 6$ B 1 6
Watched movie at 6$ Z 2 6
关于数据的一些解释:
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApplication1
{
internal class Data
{
public string Detail { get; set; }
public string Entry { get; set; }
public int Number { get; set; }
public int Rate { get; set; }
}
internal class Program
{
private static void Main(string[] args)
{
List<Data> entries = new List<Data>();
// Clubbed entry...While sorting only consider entry with Number set to 1. They will have same rate and Detail.
entries.Add(new Data() { Detail = "Carpool at 5$", Entry = "C", Number = 1, Rate = 5 });
entries.Add(new Data() { Detail = "Carpool at 5$", Entry = "H", Number = 2, Rate = 5 });
// Clubbed entry
entries.Add(new Data() { Detail = "Played Cricket at 2$", Entry = "X", Number = 1, Rate = 2 });
entries.Add(new Data() { Detail = "Played Cricket at 2$", Entry = "O", Number = 2, Rate = 2 });
// entry which have empty Entry value such entries will have Number set to 0
entries.Add(new Data() { Detail = "Done something at 4$", Entry = "", Number = 0, Rate = 4 });
// entry which will not have an pair and Number column for such entries would be set to 1 and
// there wouldn't be any entry in list which have same detail and rate with Number coloumn set to 2
entries.Add(new Data() { Detail = "Done something else at 9$", Entry = "M", Number = 1, Rate = 9 });
// Clubbed entry
entries.Add(new Data() { Detail = "Watched movie at 6$", Entry = "B", Number = 1, Rate = 6 });
entries.Add(new Data() { Detail = "Watched movie at 6$", Entry = "Z", Number = 2, Rate = 6 });
// Sorting on Entry Coloumn
var sortedList = entries.GroupBy(x => x.Detail).OrderBy(x => x.FirstOrDefault(y => y.Number <= 1).Entry).SelectMany(x => x).ToList();
}
}
}
Detail Entry Number Rate
Done something at 4$ "" 0 4
Watched movie at 6$ B 1 6
Watched movie at 6$ Z 2 6
Carpool at 5$ C 1 5
Carpool at 5$ H 2 5
Done something else at 9$ M 1 9
Played Cricket at 2$ X 1 2
Played Cricket at 2$ O 2 2
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApplication1
{
internal class Data
{
public string Detail { get; set; }
public string Entry { get; set; }
public int Number { get; set; }
public int Rate { get; set; }
}
internal class Program
{
private static void Main(string[] args)
{
List<Data> entries = new List<Data>();
// Clubbed entry...While sorting only consider entry with Number set to 1. They will have same rate and Detail.
entries.Add(new Data() { Detail = "Carpool at 5$", Entry = "C", Number = 1, Rate = 5 });
entries.Add(new Data() { Detail = "Carpool at 5$", Entry = "H", Number = 2, Rate = 5 });
// Clubbed entry
entries.Add(new Data() { Detail = "Played Cricket at 2$", Entry = "X", Number = 1, Rate = 2 });
entries.Add(new Data() { Detail = "Played Cricket at 2$", Entry = "O", Number = 2, Rate = 2 });
// entry which have empty Entry value such entries will have Number set to 0
entries.Add(new Data() { Detail = "Done something at 4$", Entry = "", Number = 0, Rate = 4 });
// entry which will not have an pair and Number column for such entries would be set to 1 and
// there wouldn't be any entry in list which have same detail and rate with Number coloumn set to 2
entries.Add(new Data() { Detail = "Done something else at 9$", Entry = "M", Number = 1, Rate = 9 });
// Clubbed entry
entries.Add(new Data() { Detail = "Watched movie at 6$", Entry = "B", Number = 1, Rate = 6 });
entries.Add(new Data() { Detail = "Watched movie at 6$", Entry = "Z", Number = 2, Rate = 6 });
// Sorting on Entry Coloumn
var sortedList = entries.GroupBy(x => x.Detail).OrderBy(x => x.FirstOrDefault(y => y.Number <= 1).Entry).SelectMany(x => x).ToList();
}
}
}
Detail Entry Number Rate
Done something at 4$ "" 0 4
Watched movie at 6$ B 1 6
Watched movie at 6$ Z 2 6
Carpool at 5$ C 1 5
Carpool at 5$ H 2 5
Done something else at 9$ M 1 9
Played Cricket at 2$ X 1 2
Played Cricket at 2$ O 2 2
问题:
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApplication1
{
internal class Data
{
public string Detail { get; set; }
public string Entry { get; set; }
public int Number { get; set; }
public int Rate { get; set; }
}
internal class Program
{
private static void Main(string[] args)
{
List<Data> entries = new List<Data>();
// Clubbed entry...While sorting only consider entry with Number set to 1. They will have same rate and Detail.
entries.Add(new Data() { Detail = "Carpool at 5$", Entry = "C", Number = 1, Rate = 5 });
entries.Add(new Data() { Detail = "Carpool at 5$", Entry = "H", Number = 2, Rate = 5 });
// Clubbed entry
entries.Add(new Data() { Detail = "Played Cricket at 2$", Entry = "X", Number = 1, Rate = 2 });
entries.Add(new Data() { Detail = "Played Cricket at 2$", Entry = "O", Number = 2, Rate = 2 });
// entry which have empty Entry value such entries will have Number set to 0
entries.Add(new Data() { Detail = "Done something at 4$", Entry = "", Number = 0, Rate = 4 });
// entry which will not have an pair and Number column for such entries would be set to 1 and
// there wouldn't be any entry in list which have same detail and rate with Number coloumn set to 2
entries.Add(new Data() { Detail = "Done something else at 9$", Entry = "M", Number = 1, Rate = 9 });
// Clubbed entry
entries.Add(new Data() { Detail = "Watched movie at 6$", Entry = "B", Number = 1, Rate = 6 });
entries.Add(new Data() { Detail = "Watched movie at 6$", Entry = "Z", Number = 2, Rate = 6 });
// Sorting on Entry Coloumn
var sortedList = entries.GroupBy(x => x.Detail).OrderBy(x => x.FirstOrDefault(y => y.Number <= 1).Entry).SelectMany(x => x).ToList();
}
}
}
Detail Entry Number Rate
Done something at 4$ "" 0 4
Watched movie at 6$ B 1 6
Watched movie at 6$ Z 2 6
Carpool at 5$ C 1 5
Carpool at 5$ H 2 5
Done something else at 9$ M 1 9
Played Cricket at 2$ X 1 2
Played Cricket at 2$ O 2 2
-
我的解决方案不考虑速率,只考虑基于细节的组,可以
可以修改吗?
- 是否可以像在我的代码中使用IComparer进行排序那样使用IComparer进行排序
FirstOrDefault
,则应处理null
返回?。
。如果您不希望组中不包含0或1的数字
,则应首先使用。您应该根据编号对组成员进行排序
:
var sortedList = entries.GroupBy(x => new { x.Detail, x.Rate })
.OrderBy(x => x.FirstOrDefault(y => y.Number <= 1)?.Entry)
.SelectMany(x => x.OrderBy(z => z.Number))
.ToList();
var sortedList=entries.GroupBy(x=>new{x.Detail,x.Rate})
.OrderBy(x=>x.FirstOrDefault(y=>y.Number x.OrderBy(z=>z.Number))
.ToList();
IMO忘了速度。你有几个条目。无论你做什么,这都会在几分之一毫秒内运行。你只是过早地优化了这里。你可以看看。这是集中排序规则的标准方法。然后,你可以按目的命名排序,并随意集中更改比较的属性@farlee2121事实上,IComparer对我来说是最好的解决方案,因为在现有代码中,所有列排序都是通过IComparer完成的,但我不知道如何在比较方法中进行分组。你能告诉我如何使用IComparer解决上述问题吗?IComparer的效率不会比你现有的更高(假设你所说的效率是指速度)。它可能会以更成功的方式做同样的事情,但不会“更快”。您的代码当前是O^n
复杂的。添加字典会使它变得O^n log n
复杂,但将数据添加到字典可能会否定任何性能改进。如果您想要真正的效率,您需要衡量整个代码,找出瓶颈并加以改进。对此进行修补可能是一种浪费时间。@Liam在我现有的代码中,所有的列排序都是使用IComparer执行的,比较方法中有一个开关大小写,它会注意需要进行哪些列排序,我去了Linq,因为我无法使用IComparer进行排序。我无法使用IComparer对条目进行分组,如果您能给我看一下,那就太好了。“OrderBy(x=>x.FirstOrDefault(y=>y.Number让我们假设您想用降序OrderByDescending
条目
来代替-您希望分组的行仍然是1,2顺序还是2,1顺序?这就是为什么您需要额外的子排序。或者,如果原始源顺序是随机的-即使使用升序排序,您也可以以2,1结束。