ListItemCollection Sort Alphabetical

Discussion in 'ASP .Net' started by =?Utf-8?B?YmVub2l0?=, Nov 3, 2005.

  1. Hi,

    I have this ListBox in which the Itemciollection can be manipulated by the
    user (items can be added one way or another, and moved to other listboxes).
    Is there any way that I can get this collection to be sorted alphabetically?
    Thx
     
    =?Utf-8?B?YmVub2l0?=, Nov 3, 2005
    #1
    1. Advertising

  2. benoit wrote:
    > Hi,
    >
    > I have this ListBox in which the Itemciollection can be manipulated by the
    > user (items can be added one way or another, and moved to other listboxes).
    > Is there any way that I can get this collection to be sorted alphabetically?
    > Thx
    >



    ListItemCollection cannot be inherited from.

    Therefore you have to write your own class implementing
    System.Collections.IComparer.

    Ideally you would make a ListItemComparer class implementing IComparer,
    like this:

    http://rafb.net/paste/results/BYaFKM60.html

    Then you would copy all the ListItem references to an ArrayList, and
    then use ArrayList.Sort with the given ListItemComparer.

    When this is done, then you would clear the items in the ListBox and
    re-add them from the ArrayList.

    --
    Emil Christopher Melar
     
    Emil Christopher Melar, Nov 3, 2005
    #2
    1. Advertising

  3. Emil Christopher Melar wrote:
    > benoit wrote:
    >
    >> Hi,
    >> I have this ListBox in which the Itemciollection can be manipulated by
    >> the user (items can be added one way or another, and moved to other
    >> listboxes).
    >> Is there any way that I can get this collection to be sorted
    >> alphabetically?
    >> Thx
    >>

    >
    >
    > ListItemCollection cannot be inherited from.
    >
    > Therefore you have to write your own class implementing
    > System.Collections.IComparer.
    >
    > Ideally you would make a ListItemComparer class implementing IComparer,
    > like this:
    >
    > http://rafb.net/paste/results/BYaFKM60.html
    >
    > Then you would copy all the ListItem references to an ArrayList, and
    > then use ArrayList.Sort with the given ListItemComparer.
    >
    > When this is done, then you would clear the items in the ListBox and
    > re-add them from the ArrayList.
    >


    I was too quick.

    Clearing the items will also make the references in the ArrayList
    non-existant.

    You have to _copy_ the ListItems in the Items property
    (ListItemCollection) to the ArrayList:

    ArrayList list = new ArrayList ( listBox.Items );

    --
    Emil Christopher Melar
     
    Emil Christopher Melar, Nov 3, 2005
    #3
  4. Performance hint:
    You should try to apply the sorting to your underlying datastructure (the
    list, dataset or whatever that you bound to the ListBox) and re-bind again,
    rather than copying the ListBox items to an array (and then sort the array).

    regards
    Emil Kvarnhammar
    http://www.ynax.com

    "benoit" <> wrote in message
    news:...
    > Hi,
    >
    > I have this ListBox in which the Itemciollection can be manipulated by the
    > user (items can be added one way or another, and moved to other
    > listboxes).
    > Is there any way that I can get this collection to be sorted
    > alphabetically?
    > Thx
    >
     
    Emil Kvarnhammar, Nov 3, 2005
    #4
  5. Are you trying to see the list sorted while adding each new item or on click
    of a button?

    if you are trying to sort while adding items to the list, (i assume you are
    using a code like ddList1.Items.Add(new ListItem("new item")) )

    1) Init a counter

    int ListItemCounter = 0;

    2) loop through the list collection and incriment ListItemCounter

    foreach (ListItem li in ddList1.Items)
    {
    ListItemCounter ++;
    3) Check if Item.Text > "new item" , if so, insert new item at
    ListItemCounter - 1

    ddList1.Items.Insert(ListItemCounter-1,new ListItem("new item")) )

    this logic will check each item currently in the list and will insert the
    new item when it find a larger value..

    The initial list should be sorted alphabetically for this to work..


    "benoit" wrote:

    > Hi,
    >
    > I have this ListBox in which the Itemciollection can be manipulated by the
    > user (items can be added one way or another, and moved to other listboxes).
    > Is there any way that I can get this collection to be sorted alphabetically?
    > Thx
    >
     
    =?Utf-8?B?U3JlZWppdGggUmFt?=, Nov 3, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kurt Mang
    Replies:
    3
    Views:
    3,978
    szabelin
    Aug 18, 2003
  2. =?Utf-8?B?UkFNQURV?=
    Replies:
    0
    Views:
    612
    =?Utf-8?B?UkFNQURV?=
    May 16, 2004
  3. Replies:
    7
    Views:
    407
  4. Pander Musubi

    Custom alphabetical sort

    Pander Musubi, Dec 24, 2012, in forum: Python
    Replies:
    8
    Views:
    193
  5. Roy Smith

    Re: Custom alphabetical sort

    Roy Smith, Dec 24, 2012, in forum: Python
    Replies:
    10
    Views:
    290
    Joshua Landau
    Dec 27, 2012
Loading...

Share This Page