C# - Order a ListBox by its' strings & Remove a piece of the string
I have a file that I am uploading to a RichTextBox and then displaying it to a ListBox after I have formatted it. The ListBox looks like this (minus some lines that I deleted so it is shorter to work with):
C44 EXCLUDES 237.910 193.469 0 0603_5
C45 EXCLUDES 244.102 193.387 0 0603_5
R47 EXCLUDES 226.935 179.519 90 0402_2
C18 CAP-00129G 230.960 190.619 0 0402_4
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_4
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
... .......... ....... ....... ... ........
Anyways, 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....). By doing this it would cause the new ListBox to be ordered in this order (although not all of these are shown in the file example):
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.
(notice the TANT3216
comes before SOT236
because it is going off of the number 3216 not TANT in the above ordering list)
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 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:
R47 EXCLUDES 226.935 179.519 90 0402_2
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
C18 CAP-00129G 230.960 190.619 0 0402_4
C56 EXCLUDES 229.430 189.374 0 0402_4
R10 EXCLUDES 246.560 203.894 0 0402_9
C44 EXCLUDES 237.910 193.469 0 0603_5
C45 EXCLUDES 244.102 193.387 0 0603_5
R42 EXCLUDES 241.010 192.194 90 TANT3216
Q7 IC-00268G 258.460 205.594 0 SOT236
... .......... ....... ....... ... ........
Once the ListBox is sorted I have a button (that is not working properly) to replace the end value of each the string line in the ListBox with a " "
. So, on the click of a button, I would like to "REMOVE ENDINGS" and re-upload it to the same ListBox.
So the same (updated) ListBox would look like this:
R47 EXCLUDES 226.935 179.519 90
C17 CAP-00129G 250.085 198.569 180
R21 CAP-00129G -123.370 -112.114 270
C25 CAP-00130G 255.635 189.669 90
C18 CAP-00129G 230.960 190.619 0
C56 EXCLUDES 229.430 189.374 0
R10 EXCLUDES 246.560 203.894 0
C44 EXCLUDES 237.910 193.469 0
C45 EXCLUDES 244.102 193.387 0
R42 EXCLUDES 241.010 192.194 90
Q7 IC-00268G 258.460 205.594 0
... .......... ....... ....... ...
NOT WORKING (CODE):
private void removePackageButton_Click(object sender, EventArgs e)
{
System.Windows.Forms.ListBox.ObjectCollection contents = placementOneListBox.Items;
foreach (string str in contents)
{
List<string> list = str.Split(' ').ToList();
if (list.Count == 6)
{
string remove = list[5];
list.Remove(remove);
placement开发者_C百科OneListBox.Items.Equals(list.ToArray());
}
}
}
QUESTIONS:
- I have no idea on how to go about this and I am looking for any help possible.
- Is there another way to go about this that is much simpler?
If you just want to do simple string manipulation, copy the ListBox's Item collection to a temporary string array, do some work on the array, and copy it back using the Items' Clear() and AddRange() methods. I like using lambda expressions for this kind of manipulation.
Sort
// Create a regular expression to identify the last column after the whitespace
Regex pattern = new Regex("\\s+\\S+$");
// Dimension an array large enough
string[] items = new string[lbContent.Items.Count];
// Copy the items
lbContent.Items.CopyTo(items, 0);
// Use an orderby to sort and convert resulting IEnumerable<> back to array
items = (from i in items
let m = pattern.Match(i).Value
orderby m.Trim()
select i).ToArray();
// Place it back in the collection
lbContent.Items.Clear();
lbContent.Items.AddRange(items);
Trim
Regex pattern = new Regex("\\s+\\S+$");
string[] items = new string[lbContent.Items.Count];
lbContent.Items.CopyTo(items, 0);
// Replace the last column with empty strings
items = (from i in items
let m = pattern.Match(i).Value
select i.Replace(m, string.Empty)).ToArray();
lbContent.Items.Clear();
lbContent.Items.AddRange(items);
Or if you're feeling bold...
Both Simultaneously
Regex pattern = new Regex("\\s+\\S+$");
string[] items = new string[lbContent.Items.Count];
lbContent.Items.CopyTo(items, 0);
items = (from i in items
let m = pattern.Match(i).Value
orderby m.Trim()
select i.Replace(m, string.Empty)).ToArray();
lbContent.Items.Clear();
lbContent.Items.AddRange(items);
==EDIT==
Both Simultaneously, Trimming the strings
Regex pattern = new Regex("\\s+\\S+$");
string[] items = new string[lbContent.Items.Count];
lbContent.Items.CopyTo(items, 0);
items = (from i in items
let t = i.Trim(),
m = pattern.Match(t).Value
orderby m
select t.Replace(m, string.Empty)).ToArray();
lbContent.Items.Clear();
lbContent.Items.AddRange(items);
精彩评论