如何按字符对TreeView输出的字典排序? [英] How to sort a dictionary for TreeView output by Character for character?

查看:54
本文介绍了如何按字符对TreeView输出的字典排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Im setting up a treeView list and need to add some new Items. Idea is to create some sort of "billing list" for each day. First the user selects a date on the left side and get a treeView displayed on the right; containing all inserted Bills. To do so, the user can select a Level, a name + amount of money to insert to. The money of all children should be summarized in the respective parent's text data be like Level| Name amount

  • 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 |
  • ....
  • ...
  • ...
  • 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
  • 3.1|
  • ....
  • ...
  • ...

因此,只要用户输入(带有3个文本框的简单弹出式表单)新值,就应该扩展树.

so the tree should be extended whenever the user put in(simple pop-up form with 3 textboxes) a new value.

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

So far, i've created a dictionary of type > The outer Dictionary is to store the items apart from each other, split into each one date. Every day could have another kind of treestructure; the only one beeing on top of every day is "0| Complete amount". the inner dictionary contains the level (0, 1.1, 1.2.2.1...) and all entries for this level.

问题从此字典的种类开始和结束. 万一它被排序并且永远不会被碰到,一切都很好. 但是,如果有什么不对的地方,我需要一种以正确的方式对字典进行排序的方法,或者以正确的方式对其进行迭代的方法.

The problems start and end with the sort of this dictionary. In case it is sorted and will never be touched, everything is ok. But if anything is not in order, I need either a way to sort the dictionary in a correct way, or to iterate over it in the right way.

1.1处于1.2之前,2之前,但1之后.

1.1 sould be before 1.2 and before 2, but after 1.

给出新的字典,例如

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

在执行"orderby"之后,它将具有相同的结构,但我需要将其设为

after i do the "orderby" it will be the same structure, but I need it to be

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

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

Does anyone know, how to reach this? Or is there a way to iterate over all items and add them as child-items in the right order? Or any way to auto-sort the treeview by .split('|')[0]? my items always start with "level + |"

推荐答案

尝试IComparable:

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



}

这篇关于如何按字符对TreeView输出的字典排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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