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


  • 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?

-------------Problems Reply------------

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;
return col2.Compare(xParts[1], yParts[1]);


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



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



Category:c# Views:0 Time:2011-07-26

Related post

  • Sorting a ListBox bound to an ObjectDataSource using a dropdown with 2 selections 2009-02-12

    I am having an issue on Sorting a ListBox with a dropdown. The dropdown will have 2 options: Sort By # Sort By Type The ListBox contains a list of items which are initially ordered in ascending order by #. It has the following format: 1: Red 2: Green

  • Sorting a ListBox in WPF 2009-07-22

    Let me start off by saying that I am completely new with WPF (this is my first project and I have been working in it for less than a week). With that being said, please be easy on me! I have three list ListBoxes that are being bound to ObservableColl

  • sorting a listbox in VBA 2010-07-05

    I have a listbox with values. Now I need to sort that listbox, BUT NOT BY ALPHABETICALLY. You see the values in a listbox are from a table. ------------------------ | name | order | size | ======================== value1 4 value2 3 value3 1 value4 2

  • VBA sort a listbox of outlook contacts 2011-01-31

    I am successfully able to populate a 2-column listbox with the contents of my outlook contacts folder and send that info to a textbox on click... alas, how can I sort the listbox? Private Sub getOutlookContacts() Dim i As Integer Dim oOutlookApp As O

  • Sorting a listbox bound to an ObservableCollection 2011-04-25

    How would one go about sorting my listbox contents? It seems to me that it would make more sense to keep this only on the UI layer since sorting won't affect my business logic, so it probably goes in the xaml or code-behind. I can't figure out what t

  • Sort WPF ListBox on button click? 2009-04-11

    First off let me just say I'm very new to coding so there are big gaps in my knowledge... anywho: Right, I'm trying to sort a WPF listbox when a button is clicked, preferrably in pure xaml (otherwise VB). I'm having a hard time seeing as most samples

  • How can I sort a ListBox using only XAML and no code-behind? 2009-08-14

    I need to sort the strings in a ListBox, but it is bound to the view model by another component via the DataContext. So I can't directly instantiate the view model in XAML, as in this example, which uses the ObjectDataProvider. In my XAML: <ListBo

  • Sort the listbox items in C# 2009-11-05

    I develop a webpage in that I display a list box items which is fetched from a database. Dynamically I added some items into it. It adds to the end of the list box, so I want to sort the list box item after I add items. I tried Arraylist for sorting,

  • C#, BindingList, BindingSource, sorting a ListBox? 2011-05-04

    I already googled for an hour now and didn't find a working answer. I have a myClass with different members such as a string Name and some int Numbers. Then I have a myList of type List and then I have a Listbox control. I can easily display the List

  • Sort a listbox after being dynamically allocated 2011-08-09

    I am pulling data in from a query, where three of the columns are being put into the same Listbox. Does anyone know of a sort statement to sort it alphabetically after inserting the data into a listbox, or would I be better putting the results of my

  • Border around a range of cells in a sorted WPF ListBox 2011-05-17

    I have a horizontal ListBox of items, each of which has two properties: Name, and Parent. Several items have the same Parent, and some items have no parent. I would like to present the list with items ordered by Parent if they have one, otherwise by

  • C# - Sorting a ListBox containing key/value pairs using LINQ 2011-09-01

    I have a list box control that contains key value pairs delimited by an "=" sign. Example: hot=cold fast=slow high=low blue=red I have two buttons, one that will sort the list by key and the other will sort the list by pair. How could I do this using

  • WPF VB.net ListBox how to get selected item index or a sorted & grouped list relative to item source 2011-02-12

    Good evening all, Firstly thank you for the time taken to read this. I'm having some difficulty with a sorted & grouped listbox in WPF vb.net 3.5 that has an items source bound to an ObservableCollection. What I want to be able to do is to retrie

  • C#'s ListBox doesn't see my overrided Sort() method 2011-05-28

    I'm trying to override Sort() method in my custom control. When my control contains ListBox and then I override Sort() method, everything works. But when I want my ListBox (1) to be extended by another ListBox (2), that contains Sort() method, and th

  • Access Listbox based on value list - sorting on column 2011-10-12

    I have a Listbox with 3 columns on an access form which has as it's row source a value list (not a recordset from the db), which is passed as comma seperated string. The third column is a numeric value, and I wish to sort the listbox desc on that thi

  • Unable to save ListBox Items in sorted order 2012-01-19

    In my .NET web forms page, I have a ListBox and some jQuery that allows me to sort the items. I select several items and then loop over them to save to my database. I want to save the order the items are sorted in, however, I am unable to get it to w

  • Filter/Sort Listbox Items 2012-03-06

    I have a listbox with items filled with stackpanels such as: <StackPanel Orientation="Horizontal" Margin="5"> <TextBlock x:Name="accessionNumber" Text="{Binding Path=accessionNumber}" /> <TextBlock Text="{Binding Path=orderRadDept}" Ma

  • App crashes on changing tabs that contain ListBoxes with ItemTemplates 2008-12-01

    I have a WPF TabControl with two TabItems. Each TabItem contains a ListBox with a separate ObservableCollection as its ItemsSource. Each ListBox has a different ItemTemplate. No matter which TabItem I set to be selected at startup, that tab will be d

  • Sort combobox values alphabetically 2009-05-13

    I have a combobox in a userform for excel. What is the easiest way to sort it alphabetically? The values for it are hardcoded in vba and new ones are just added to the bottom so they are not in any kind of order already. The userform is currently bei

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.181 (s). 11 q(s)