Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何按字符对TreeView输出的字典进行排序?_C#_Sorting_Dictionary_Treeview - Fatal编程技术网

C# 如何按字符对TreeView输出的字典进行排序?

C# 如何按字符对TreeView输出的字典进行排序?,c#,sorting,dictionary,treeview,C#,Sorting,Dictionary,Treeview,我正在设置一个树状视图列表,需要添加一些新项目。这个想法是为每天创建某种“账单列表”。 首先,用户在左侧选择一个日期,并在右侧显示一个树状视图;包含所有插入的票据。 为此,用户可以选择一个级别,一个名称+插入的金额。所有孩子的钱应该在各自家长的文本数据中汇总如下 级别|名称金额 0 |全额xxx欧元1 |购物25欧元(计算) 1.1阿尔迪20欧元(插入) 1.2 Lidl 5欧元(插入) 1.2.1米尔卡3欧元(插入) 1.2.2 |巴斯2€(计算) 1.2.2.1 |牙刷1欧元(插入) 1.

我正在设置一个树状视图列表,需要添加一些新项目。这个想法是为每天创建某种“账单列表”。 首先,用户在左侧选择一个日期,并在右侧显示一个树状视图;包含所有插入的票据。 为此,用户可以选择一个级别,一个名称+插入的金额。所有孩子的钱应该在各自家长的文本数据中汇总如下 级别|名称金额

  • 0 |全额xxx欧元1 |购物25欧元(计算)
  • 1.1阿尔迪20欧元(插入)
  • 1.2 Lidl 5欧元(插入)
  • 1.2.1米尔卡3欧元(插入)
  • 1.2.2 |巴斯2€(计算)
  • 1.2.2.1 |牙刷1欧元(插入)
  • 1.2.2.2 |肥皂1欧元(插入)2 |汽车100欧元(计算)
  • 2.1 |燃料80欧元(插入)
  • 2.2 |洗衣20欧元(插入)3 |晚餐
  • 3.1|
因此,每当用户输入一个新值(带有3个文本框的简单弹出表单)时,应该扩展树

到目前为止,我已经创建了一个类型> 外部字典是将项目分开存储,分为每个日期。每天都可以有另一种树状结构;每天最重要的是“0 |全数”。 内部字典包含级别(0、1.1、1.2.2.1…)和该级别的所有条目

这些问题从这本词典的类型开始到结束。 如果它被分类,永远不会被触摸,一切都是好的。 但是,如果有什么不符合顺序,我需要一种方法以正确的方式对字典进行排序,或者以正确的方式对它进行迭代

1.1应该在1.2之前和2之前,但在1之后

考虑到这本新字典

  • 一,|
  • 1.1|
  • 二,|
  • 1.2|
  • 2.1|
在我完成“orderby”之后,它将是相同的结构,但我需要它是相同的

  • 一,|
  • 1.1|
  • 1.2|
  • 二,|
  • 2.1|
有人知道,怎么做到这一点吗? 或者有没有一种方法可以迭代所有项并按正确的顺序将它们作为子项添加?或者以任何方式自动排序treeview by.split(“|”)[0]?我的项目总是以“级别+|”开头。

试试IComparable:

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


namespace ConsoleApplication131
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] inputs = {
                "0| Complete amount xxx€ 1| Shopping 25€ (calculated)",
                "1.1| Aldi 20€ (inserted)",
                "1.2| Lidl 5€ (inserted)",
                "1.2.1| Milka 3€ (inserted)",
                "1.2.2| Bath 2€ (calculated)",
                "1.2.2.1| Toothbrush 1€ (inserted)",
                "1.2.2.2| Soap 1€ (inserted) 2| Car 100€ (calculated)",
                "2.1| Fuel 80€ (inserted)",
                "2.2| washing 20€ (inserted) 3| Dinner"
                             };
            SortParagraph sorter = new SortParagraph();
            List<SortParagraph> sortParagraphs = sorter.ParsePararaph(inputs);

            List<SortParagraph> sortedParagraphs = sortParagraphs.OrderBy(x => x).ToList();

            foreach (SortParagraph sortParagraph in sortedParagraphs)
            {
                Console.WriteLine("Para : '{0}', Titles = '{1}'", string.Join(".", sortParagraph.subParagraphs), string.Join(",", sortParagraph.titles));
            }
            Console.ReadLine();

        }
    }
    public class SortParagraph : IComparable<SortParagraph>
    {
        public int[] subParagraphs { get; set; }
        public string[] titles { get; set; }

        public List<SortParagraph> ParsePararaph(string[] inputs)
        {
            List<SortParagraph> paragraphs = new List<SortParagraph>();
            foreach(string input in inputs)
            {
                SortParagraph newParagraph = new SortParagraph();
                string[] splitParagraph = input.Split(new char[] { '|' }).ToArray();
                newParagraph.titles = splitParagraph.Skip(1).ToArray();
                newParagraph.subParagraphs = splitParagraph.First().Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries).Select(x => int.Parse(x)).ToArray();
                paragraphs.Add(newParagraph);
            }

            return paragraphs;
        }
        public int CompareTo(SortParagraph other)
        {

            int minSize = Math.Min(this.subParagraphs.Length, other.subParagraphs.Length);
            for (int i = 0; i < minSize; i++)
            {
                if (this.subParagraphs[i] != other.subParagraphs[i])
                {
                    if (this.subParagraphs[i] < other.subParagraphs[i])
                    {
                        return -1;
                    }
                    else
                    {
                        return 1;
                    }
                }
            }
            if (this.subParagraphs.Length == other.subParagraphs.Length)
            {
                return 0;
            }
            else
            {
                if (this.subParagraphs.Length < other.subParagraphs.Length)
                {
                    return -1;
                }
                else
                {
                    return 1;
                }
            }
        }
    }



}
使用系统;
使用System.Collections.Generic;
使用系统集合;
使用System.Linq;
使用系统文本;
命名空间控制台应用程序131
{
班级计划
{
静态void Main(字符串[]参数)
{
字符串[]输入={
“0 |全额xxx欧元1 |购物25欧元(计算)”,
“1.1阿尔迪20欧元(插入)”,
“1.2 Lidl 5欧元(插入)”,
“1.2.1米尔卡3欧元(插入)”,
“1.2.2 |巴斯2€(计算)”,
“1.2.2.1牙刷1欧元(插入)”,
“1.2.2.2 |肥皂1欧元(插入)2 |汽车100欧元(计算)”,
“2.1 |燃料80欧元(插入)”,
“2.2 |洗衣20欧元(插入)3 |晚餐”
};
SortParagraph sorter=新SortParagraph();
列表sortParagraphs=分类器.ParseParagraphs(输入);
List sortedParagraphs=sortParagraphs.OrderBy(x=>x.ToList();
foreach(SortParagraph SortParagraph中的SortParagraph)
{
Console.WriteLine(“Para:'{0}',Titles='{1}',string.Join(“.”,sortParagraph.subParagraphs),string.Join(“,”,sortParagraph.Titles));
}
Console.ReadLine();
}
}
公共类SortParagraph:IComparable
{
公共int[]子段落{get;set;}
公共字符串[]标题{get;set;}
公共列表ParseParapraph(字符串[]输入)
{
列表段落=新列表();
foreach(输入中的字符串输入)
{
SortParagraph newParagraph=新的SortParagraph();
string[]splitparagration=input.Split(新字符[]{'|'}).ToArray();
newparation.titles=splitparation.Skip(1.ToArray();
newparagraphs=splitparagraphs.First().Split(新字符[]{.'},StringSplitOptions.RemoveEmptyEntries)。选择(x=>int.Parse(x)).ToArray();
添加(新的段落);
}
返回段落;
}
公共int比较(SortParagraph其他)
{
int minSize=Math.Min(this.subParagraphs.Length,other.subParagraphs.Length);
对于(int i=0;i
我将jdweng的答案标记为正确,因为我得到了缺少的提示。关键词“Icomparer”。 这个解决方案现在运行良好,除非我不需要删除任何项目(我不需要,因为我将在删除的情况下重建完整的列表)

类DuplicateKeyComparer:IComparer,其中TKey:IComparable
{
公共整数比较(TKey x,TKey y)
{
int结果=x。与(y)相比;
如果(结果==0)
返回1;//将相等处理为更大
其他的
返回结果;
}
}
是。唐
class DuplicateKeyComparer<TKey>:IComparer<TKey> where TKey : IComparable
    {
        public int Compare(TKey x, TKey y)
        {
            int result = x.CompareTo(y);

            if (result == 0)
                return 1;   // Handle equality as beeing greater
            else
                return result;
        }
    }