开发者

C# - Sorting a ListBox by the ending

I have a ListBox that contains some data like below:

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
...     ..........  .......  .......  ...  ........

I would like to sort the ListBox by the ending on the strings... so the values in the 6th column (0603_5, 0603_5, 0402_2, 0402_4, 0402_3, TANT3216, 0402_9....).


Although not all of these are shown in the file example above, here is the order of which they should appear (top to bottom):

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.

Also, if there are multiple endings that are similar (see *0402_3* above and below), then the list item will be sorted by the 2nd column. So even though the line开发者_如何学Python beginning with R21 comes after the line beginning with C25, and they both end with *0402_3*, R21 will be placed above C25 because it the 2nd column is checked after the 6th column (this is sorted from smallest to largest).

SO, the new file would look like this:

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
...     ..........  .......  .......  ...  ........

Notice the TANT3216 comes before SOT236 because it is going off of the number 3216 not TANT in the above ordering list.


QUESTIONS:

  • How can I properly sort the first file to look like the second file using the 6th column (and if needed the 2nd column)?
  • Could I use Regex or is there a simpler way?
  • I have 3 ListBoxes, this 1 needs to be sorted using the rules (endings) above, the 2nd ListBox uses seperate rules, and the 3rd ListBox sorts anything else that did not go into the 1st or 2nd ListBox. So the 1st and the 2nd ListBoxes are similar, so how could I go about sorting anything by its endings alphabetically for the 3rd ListBox?


Step 1:

Create a class with properties for your columns. Maybe you already have that, it's not clear. Otherwise you'll have to break up the strings.

Step 2:

Create a LINQ query that uses OrderBy(x=> x.Col6Property).ThenBy(x=>x.Col2Property)

Step 3:

Add the List of objects to your Listbox and use an overridden ToString() or ListBox.Format to get the output you want.


You should do this by implementing the IComparer interface for the type that you are dealing with. If the data is all tab separated strings, then you could use the following:

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
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜