简体   繁体   English

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

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

Im setting up a treeView list and need to add some new Items. 我设置了一个treeView列表,需要添加一些新项。 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; 首先,用户在左侧选择一个日期,并在右侧显示一个treeView; 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| 所有孩子的钱都应汇总在各自父母的文本数据中,例如Level |。 Name amount 名称金额

  • 0| 0 | Complete amount xxx€ 1| 全部金额xxx€1 | Shopping 25€ (calculated) 购物25€(已计算)
  • 1.1| 1.1 | Aldi 20€ (inserted) Aldi 20€(已插入)
  • 1.2| 1.2 | Lidl 5€ (inserted) Lidl 5€(已插入)
  • 1.2.1| 1.2.1 | Milka 3€ (inserted) Milka 3€(已插入)
  • 1.2.2| 1.2.2 | Bath 2€ (calculated) 浴2€(计算)
  • 1.2.2.1| 1.2.2.1 | Toothbrush 1€ (inserted) 牙刷1€(已插入)
  • 1.2.2.2| 1.2.2.2 | Soap 1€ (inserted) 2| 香皂1€(已插入)2 | Car 100€ (calculated) 车100€(已计算)
  • 2.1| 2.1 | Fuel 80€ (inserted) 燃料80€(已插入)
  • 2.2| 2.2 | washing 20€ (inserted) 3| 洗涤20€(已插入)3 | Dinner 晚餐
  • 3.1| 3.1 |
  • .... ....
  • ... ...
  • ... ...

so the tree should be extended whenever the user put in(simple pop-up form with 3 textboxes) a new value. 因此,只要用户输入(带有3个文本框的简单弹出式表单)新值,就应该扩展树。

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. 到目前为止,我已经创建了一个类型>>的字典。外部Dictionary是将项目彼此分开存储,并分成每个日期。 Every day could have another kind of treestructure; 每天可能会有另一种树形结构。 the only one beeing on top of every day is "0| Complete amount". 每天唯一的一只蜜蜂是“ 0 |完整金额”。 the inner dictionary contains the level (0, 1.1, 1.2.2.1...) and all entries for this level. 内部词典包含级别(0、1.1、1.2.2.1 ...)和该级别的所有条目。

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 sould be before 1.2 and before 2, but after 1. 1.1处于1.2之前,2之前,1之后。

Given the new dictionary like 鉴于新字典像

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

after i do the "orderby" it will be the same structure, but I need it to be 在我执行“ orderby”之后,它将是相同的结构,但是我需要它是

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

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]? 或通过.split('|')[0]自动排序树视图的任何方法? my items always start with "level + |" 我的项目始终以“ level + |”开头

Try IComparable : 试试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;
                }
            }
        }
    }



}

I marked jdweng's answer as correct, because I've got the missing hint. 我将jdweng的答案标记为正确,因为我缺少提示。 keyword "Icomparer". 关键字“比较器”。 this solution now works fine, unless i dont need to remove any item (I dont need to, because Im goin to rebuild the complete list in case of removal) 此解决方案现在可以正常工作,除非我不需要删除任何项目(我不需要,因为我将在删除的情况下重新构建完整列表)

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