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|
- 一,|
- 1.1|
- 二,|
- 1.2|
- 2.1|
- 一,|
- 1.1|
- 1.2|
- 二,|
- 2.1|
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;
}
}