簡體   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