繁体   English   中英

如何按字符对TreeView输出的字典排序?

[英]How to sort a dictionary for TreeView output by Character for character?

我设置了一个treeView列表,需要添加一些新项。 想法是每天创建某种“账单”。 首先,用户在左侧选择一个日期,并在右侧显示一个treeView; 包含所有插入的法案。 为此,用户可以选择一个级别,名称和要插入的金额。 所有孩子的钱都应汇总在各自父母的文本数据中,例如Level |。 名称金额

  • 0 | 全部金额xxx€1 | 购物25€(已计算)
  • 1.1 | Aldi 20€(已插入)
  • 1.2 | Lidl 5€(已插入)
  • 1.2.1 | Milka 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个文本框的简单弹出式表单)新值,就应该扩展树。

到目前为止,我已经创建了一个类型>>的字典。外部Dictionary是将项目彼此分开存储,并分成每个日期。 每天可能会有另一种树形结构。 每天唯一的一只蜜蜂是“ 0 |完整金额”。 内部词典包含级别(0、1.1、1.2.2.1 ...)和该级别的所有条目。

问题开始和结束于该词典的种类。 万一它被排序并且永远不会被触摸,一切都很好。 但是如果没有什么顺应性,我需要一种以正确的方式对字典进行排序的方法,或者以正确的方式对其进行迭代。

1.1处于1.2之前,2之前,1之后。

鉴于新字典像

  • 1 |
  • 1.1 |
  • 2 |
  • 1.2 |
  • 2.1 |

在我执行“ orderby”之后,它将是相同的结构,但是我需要它是

  • 1 |
  • 1.1 |
  • 1.2 |
  • 2 |
  • 2.1 |

有谁知道,如何做到这一点? 还是有一种方法可以遍历所有项目并以正确的顺序将它们添加为子项目? 或通过.split('|')[0]自动排序树视图的任何方法? 我的项目始终以“ level + |”开头

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



}

我将jdweng的答案标记为正确,因为我缺少提示。 关键字“比较器”。 此解决方案现在可以正常工作,除非我不需要删除任何项目(我不需要,因为我将在删除的情况下重新构建完整列表)

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM