繁体   English   中英

C# - 按结尾对ListBox进行排序

[英]C# - Sorting a ListBox by the ending

我有一个包含以下数据的ListBox:

C44     EXCLUDES    237.910  193.469  0    0603_5
C45     EXCLUDES    244.102  193.387  0    0603
R47     EXCLUDES    226.935  179.519  90   0402_1
C18     CAP-00129G  230.960  190.619  0    0402
C17     CAP-00129G  250.085  198.569  180  0402_3
Q7      IC-00268G   258.460  205.594  0    SOT236
C25     CAP-00130G  255.635  189.669  90   0402_3
C56     EXCLUDES    229.430  189.374  0    0402
R42     EXCLUDES    241.010  192.194  90   TANT3216
R21     CAP-00129G -123.370 -112.114  270  0402_3
R10     EXCLUDES    246.560  203.894  0    0402_9
...     ..........  .......  .......  ...  ........

我想通过字符串的结尾对ListBox进行排序...所以第6列中的值(0603_5,0603_5,0402_2,0402_4,0402_3,TANT3216,0402_9 ....)。


虽然上面的文件示例中并未显示所有这些,但以下是它们应显示的顺序(从上到下):

RES, 0402, 0201, 0603, 0805, 1206, 1306, 1608, 3216, 2551, 1913, 1313, 2513, 5125, 2525, 5619, 3813, 1508, 6431, 2512, 1505, 2208, 1005, 1010, 2010, 0505, 0705, 1020, 1812, 2225, 5764, 4532, 1210, 0816, 0363, SOT.

此外,如果有多个相似的结尾(请参阅* 0402_3 *上方和下方),则列表项将按第2列排序。 因此,即使从R21开始的行在以C25开头的行之后,并且它们都以* 0402_3 *结束, R21将被置于C25之上,因为它在第6列之后检查第2列(这是从最小到最大排序) )。

那么,新文件看起来像这样:

C18     CAP-00129G  230.960  190.619  0    0402
C56     EXCLUDES    229.430  189.374  0    0402
R47     EXCLUDES    226.935  179.519  90   0402_1
C17     CAP-00129G  250.085  198.569  180  0402_3
R21     CAP-00129G -123.370 -112.114  270  0402_3
C25     CAP-00130G  255.635  189.669  90   0402_3
R10     EXCLUDES    246.560  203.894  0    0402_9
C45     EXCLUDES    244.102  193.387  0    0603
C44     EXCLUDES    237.910  193.469  0    0603_5
R42     EXCLUDES    241.010  192.194  90   TANT3216
Q7      IC-00268G   258.460  205.594  0    SOT236
...     ..........  .......  .......  ...  ........

请注意, TANT3216SOT236之前,因为它在上面的订购列表中没有TENT的数字3216


问题:

  • 如何使用第6列(如果需要第2列)正确排序第一个文件看起来像第二个文件?
  • 我可以使用正则表达式还是有更简单的方法?
  • 我有3个ListBox,这个1需要使用上面的规则(结尾)进行排序,第二个ListBox使用单独的规则,第三个ListBox排序任何其他没有进入第一个或第二个ListBox的东西。 所以第一个和第二个ListBox是相似的,那么我怎么能按字母顺序为第三个ListBox排序任何东西呢?

第1步:

创建一个包含列属性的类。 也许你已经拥有它,但目前尚不清楚。 否则你将不得不分解字符串。

第2步:

创建一个使用OrderBy(x=> x.Col6Property).ThenBy(x=>x.Col2Property)的LINQ查询OrderBy(x=> x.Col6Property).ThenBy(x=>x.Col2Property)

第3步:

将对象列表添加到列表框中,并使用重写的ToString()或ListBox.Format来获取所需的输出。

您应该通过为您正在处理的类型实现IComparer接口来执行此操作。 如果数据是所有制表符分隔的字符串,那么您可以使用以下内容:

public class DropBoxStringComparer : IComparer<string>
{
    #region Implementation of IComparer<in string>
    Col2StringComparer col2 = new Col2StringComparer();
    Col6StringComparer col6 = new Col6StringComparer();
    public int Compare(string x, string y)
    {
        char[] tab = new[]{(char) 9};
        string[] xParts = x.Split(tab);
        string[] yParts = y.Split(tab);
        var c6compare = col6.Compare(xParts[5], yParts[5]);
        if (c6compare != 0)
        {
            return c6compare;
        }
        else
        {
            return col2.Compare(xParts[1], yParts[1]);
        }
    }

    #endregion
}

public class Col6StringComparer : IComparer<string>
{
    #region Implementation of IComparer<in string>

    public int Compare(string x, string y)
    {
        //Rules that determine order of col 6

    }

    #endregion
}

public class Col2StringComparer : IComparer<string>
{
    #region Implementation of IComparer<in string>

    public int Compare(string x, string y)
    {
        //Rules that determine order of col 2

    }

    #endregion
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM