【问题标题】:Sort items in a list对列表中的项目进行排序
【发布时间】:2020-01-29 21:58:16
【问题描述】:

我正在处理的 C# ASP.net 项目中有一个字符串列表。 列表中的内容如下所示:

Apple,1,monday
Banana,5,monday
Pear,4,tuesday
Apple,2,tuesday,
Banana,7,tuesday
Orange,6,wednesday
Apple,9,thursday,
Banana,2,friday

该列表包含一种水果项目、该特定水果的数量以及需要该水果的日期和数量。 我想要做的是根据水果的类型对列表进行排序,然后从周一到周五,每天的每个数量。 例如,对于水果项目“Apple”,我想要一个看起来像这样的表格条目。

Apple,1,2,0,9,0 

对 Apple 项目进行排序,以便显示星期一所需的苹果数量,如果某一天(例如星期三和星期五)没有苹果数量,则显示 0。

我也喜欢同样适用于列表中的所有项目等 o 同样适用于香蕉、梨、橙子或任何其他可能在列表中的水果。

任何帮助将不胜感激。

到目前为止,我得到了下面的代码,这是一种我无法工作的凌乱方法。 原始列表称为“itemsFruitMon”,排序后的列表称为“格式化”。这不是一个好方法,我被困在如何继续下去。

            foreach (var item in itemsFruitMon)
        {
            foreach (var itemComp in itemsFruitMon)
            {
                if(item.Split(',')[0]==itemComp.Split(',')[0])
                {

                    if (itemComp.Contains("monday"))
                    {
                        formatted.Add(item.Split(',')[0] + "," + item.Split(',')[1].Split(',')[0]);  
                       // formatted.Add(item.Split(',')[0]+","+item.Split(',')[1].Split(',')[0]);                           
                    }
                    else
                    {
                        formatted.Add(item.Split(',')[0] + "," + 0);
                    }
                }

            }

【问题讨论】:

  • 我们可以帮助您。您能否与我们分享您迄今为止所做的尝试,并描述您遇到的具体问题?
  • 您好,艾米,感谢您的回复。我唯一能用这个列表做的就是得到水果的总数,不管是哪天或水果类型。尝试以我想要的方式对列表进行排序时,我不知所措。我知道我想要什么只是不知道如何得到它。谢谢
  • 那么这其中的哪一部分与排序有关,您已经拥有了哪些代码?
  • @cfcorp 我们很乐意为您提供帮助,但我们不太愿意为您工作。请与我们分享您所拥有的。
  • 你考虑过使用类吗?属性可以是 Name(字符串),然后是一堆 int 属性 qtyMon、qtyTue、qtyWed、qtyThu、qtyFri。

标签: c# list sorting


【解决方案1】:

您无需在此处进行任何排序。

工作日数始终为 7。水果未到的天数需要 0。因此,对于每个水果,您需要一个 7 元素数组或整数列表。每个工作日名称都是该数组或列表中的索引。

解析器大致如下所示:

var fruitDays = new Dictionary<string, int[]>();
foreach (var line in listOfLines)
{
    var fields = line.Split(',');
    var fruit = fields[0];
    var quantity = int.Parse(fields[1]);
    var weekday = (int)Enum.Parse(typeof(DayOfWeek), fields[2], true); // Case-weekday insensitive parsing. Keep in mind that Monday=0 and Sunday=0 here 
    if (!fruitDays.ContainsKey(fruit)) fruitDays.Add(fruit, new int[7]);
    fruitDays[fruit][weekday] = quantity;
}

【讨论】:

  • 您好,感谢您的回复和帮助。 'WeekdayToIndex' 是 C# ASP.net 无法识别的内置类或库吗?谢谢
  • 谢谢可选选项,效果很好。我不得不将“WeekdayToIndex”更改为 var weekday = ((int)Enum.Parse(typeof(DayOfWeek), (day)));其中 'day' 等于特定字段的日期值。我还更改了日期以显示大写字母的第一个字母,例如周一至周一。非常感谢!!!!!!
  • 添加了正确的工作日解析。您应该仔细使用 DayOfWeek 枚举,因为在您的问题中假设星期一的索引为 0,而 DayOfWeek 枚举从 Sunday=0 开始
【解决方案2】:

尝试 IComparable 然后创建字典

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Fruit> fruits = new List<Fruit>() {
                new Fruit() { fruit =  "Apple", quantity = 1, day = "monday"},
                new Fruit() { fruit =  "Banana", quantity = 5, day = "monday"},
                new Fruit() { fruit =  "Pear", quantity = 4, day = "tuesday"},
                new Fruit() { fruit =  "Apple", quantity = 2, day = "tuesday"},
                new Fruit() { fruit =  "Banana", quantity = 7, day = "tuesday"},
                new Fruit() { fruit =  "Orange", quantity = 6, day = "wednesday"},
                new Fruit() { fruit =  "Apple", quantity = 9, day = "thursday"},
                new Fruit() { fruit =  "Banana", quantity = 2, day = "friday"}
            };

            List<Fruit> results = fruits.OrderBy(x => x).ToList();

            Dictionary<string, List<Fruit>> dict = results.GroupBy(x => x.fruit, y => y)
                .ToDictionary(x => x.Key, y => y.ToList());
        }
    }
    public class Fruit : IComparable<Fruit>
    {
        public string fruit { get; set; }
        public int quantity { get; set; }
        dayofweek date { get; set; }
        public string day {
            get { return date.ToString();}
            set { date = (dayofweek)Enum.Parse(typeof(dayofweek), value);}
        }

        enum dayofweek
        {
            sunday = 0,
            monday = 1,
            tuesday = 2,
            wednesday = 3,
            thursday = 4,
            friday = 5,
            saturday = 6
        }

        public int CompareTo(Fruit other)
        {
            if (this.fruit != other.fruit)
            {
                return this.fruit.CompareTo(other.fruit);
            }
            else
            {
                if (this.date != other.date)
                {
                    return this.date.CompareTo(other.date);
                }
                else
                {
                    return this.quantity.CompareTo(other.quantity);
                }
            }

        }
    }
}

【讨论】:

    猜你喜欢
    • 2011-04-09
    • 1970-01-01
    • 1970-01-01
    • 2015-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-02
    相关资源
    最近更新 更多