对列表中的项目进行排序

时间:2020-01-29 21:58:16

标签: c# list sorting

我正在处理的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。

Id也类似于应用于列表中所有项目的ID,对于香蕉,梨,橙子或列表中可能存在的其他水果也是如此。

任何帮助将不胜感激。

到目前为止,我已经在下面编写了他的代码,这是我无法正常工作的混乱方法。 原始列表称为“ itemsFruitMon”,排序后的列表ive称为“格式化”。这不是一个好方法,而且我对如何继续深感困惑。

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

            }

2 个答案:

答案 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);
                }
            }

        }
    }
}

答案 1 :(得分:2)

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

工作日始终为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;
}