基于某些条件排序列表c# [英] Sorting List Based on some conditions c#

查看:59
本文介绍了基于某些条件排序列表c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以最有效的方式对Entry列上给定列表中的条目进行排序。以下是我的i条目的示例。

I want to sort entries in a given list on Entry column in most efficient way. Below is the example how my i entries looks like.

#No 详情                               条目         数         价格$
1 拼车5美元                      C              1                    5

拼车5美元                      H              2                    5



2 以2美元的价格玩板球            X              1                    2

以2美元的价格玩板球            O              2                    2



3 完成4 $的东西         ""              0                    4



4 以9 $完成其他内容    M              1                    9



5 观看电影6美元            B              1                    6

观看电影6美元            ž              2                    6

#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

关于数据的一些解释:

- #没有列不可用在列表中。我刚刚提到这里提供有关条目的详细信息。

- #1,2,5是特殊的俱乐部条目,应该在条目上排序,其数字列设置为1.这些条目具有他们的特殊性速率和描述是相同的,数字将是1和2.

- 有可能存在一些具有空条目值的条目,这样的条目将数字设置为0.例如# 3

- 有可能存在一些条目具有单一条目值并且不会有一对条目,并且这些条目的数字列将被设置为1并且不会有任何条目具有相同描述和速率的列表,其中数字列
设置为2.例如#4。

- 在Entry列上排序俱乐部条目时,只考虑条目号码设置为1和其他条目应标记。



我想按照上述规则按升序或降序对Entry列进行排序。

Some explanation about data:
-#No column is not available in list. I just mentioned here to give detail about entries.
-#1,2,5 are special clubbed entries and should be sorted together on Entry which have number column set to 1. These entries have specialty that their rate and description would be same and Number would be 1 and 2.
- There is possibility that there could be some entries which have empty Entry values, such entries will have number set to 0. Such as #3
-There is possibility that there could be some entries which have single Entry value and 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 description and rate with Number column set to 2. Such as #4.
-While sorting clubbed entries on Entry column only consider Entry with Number set to 1 and other entry should tag along.

I want to sort Entry column by ascending or descending order abiding above rules.

我的解决方案:

    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();
    
            }
        }
    }

我的问题:

推荐答案

嗨A.Learn,

Hi A.Learn,

感谢您在此发帖。

我测试您的代码。你可以得到你想要的东西。 

I test your code. You could get what you want. 

>>我的解决方案不考虑费率而只考虑基于细节的组,是否可以修改?

>>My solution do not consider Rate and only groups based on Detail, can it be modified?

根据您的说明,您可以根据费率对列表进行排序。如果您不想考虑这个问题,您希望如何对列表进行排序?

Based on your description, you sort the list according to the Rate. If you do not want to consider about that, how do you want to sort the list?

>>可以使用IComparer完成,因为我的其余代码已使用IComparer完成排序?

>>Can it be done using IComparer as rest of my code had sorting done using IComparer?

GroupBy使用  IEqualityComparer< T> 。 此接口仅支持相等比较。用于排序和排序的比较
的定制由IComparer< T>提供。通用界面。

The GroupBy use IEqualityComparer<T>. This interface supports only equality comparisons. Customization of comparisons for sorting and ordering is provided by the IComparer<T> generic interface.

最好的问候,

Wendy


这篇关于基于某些条件排序列表c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆