如何按字符为字符对 TreeView 输出的字典进行排序?
How to sort a dictionary for TreeView output by Character for character?
我正在设置 treeView 列表,需要添加一些新项目。想法是为每一天创建某种 "billing list"。
首先用户select在左侧输入一个日期,然后在右侧显示一个treeView;包含所有插入的账单。
为此,用户可以 select 一个级别,一个名称 + 金额来插入。所有 children 的钱应该汇总在各自 parent 的文本数据中,就像
等级|名额
- 0|完成金额 xxx€ 1|购物 25 欧元(已计算)
- 1.1|阿尔迪 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 个文本框的简单 pop-up 表单)新值,就应该扩展树。
到目前为止,我已经创建了一个类型的字典 >
外层Dictionary是把items分开存放,拆分成一个个日期。每一天都可以有另一种树结构;每天顶部唯一的一只蜜蜂是“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|
有谁知道,如何达到这个目的?
或者有没有办法遍历所有项目并以正确的顺序将它们添加为 child-items ?或者通过 .split('|')[0] auto-sort 树视图的任何方式?我的项目总是以 "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 的回答标记为正确,因为我得到了缺失的提示。关键字 "Icomparer"。
这个解决方案现在工作正常,除非我不需要删除任何项目(我不需要,因为我要重建完整列表以防删除)
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;
}
}
我正在设置 treeView 列表,需要添加一些新项目。想法是为每一天创建某种 "billing list"。 首先用户select在左侧输入一个日期,然后在右侧显示一个treeView;包含所有插入的账单。 为此,用户可以 select 一个级别,一个名称 + 金额来插入。所有 children 的钱应该汇总在各自 parent 的文本数据中,就像 等级|名额
- 0|完成金额 xxx€ 1|购物 25 欧元(已计算)
- 1.1|阿尔迪 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 个文本框的简单 pop-up 表单)新值,就应该扩展树。
到目前为止,我已经创建了一个类型的字典 > 外层Dictionary是把items分开存放,拆分成一个个日期。每一天都可以有另一种树结构;每天顶部唯一的一只蜜蜂是“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|
有谁知道,如何达到这个目的? 或者有没有办法遍历所有项目并以正确的顺序将它们添加为 child-items ?或者通过 .split('|')[0] auto-sort 树视图的任何方式?我的项目总是以 "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 的回答标记为正确,因为我得到了缺失的提示。关键字 "Icomparer"。 这个解决方案现在工作正常,除非我不需要删除任何项目(我不需要,因为我要重建完整列表以防删除)
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;
}
}