0

enter image description hereI'm attempting to re-sort my list when adding/removing from it by storing to a list and then sorting it. When I run my application, the new items added to the list are always added to the bottom instead of sorted.

FileCabinetsRetreived is my first ListBox and FileCabinetsToAdd is my second list box.

    private void addBttn_Click(object sender, RoutedEventArgs e)
    {
        List<string> allItems = new List<string>();
        List<string> addedItems = new List<string>();
        List<string> remainingItems = new List<string>();

        //adding all items in FileCabinetsretreived into allItems list
        foreach (var item in FileCabinetsRetrieved.Items)
        {
            allItems.Add(item.ToString());
        }

        //sorting all items list
        allItems = allItems.OrderBy(x => x).ToList();

        //adding Selected items to addedItems list
        foreach (var item in FileCabinetsRetrieved.SelectedItems)
        {
            addedItems.Add(item.ToString());
        }

        //sorting addedItems list
        addedItems = addedItems.OrderBy(x => x).ToList();

        // creating list of remaining items = allitems - addedItems
        remainingItems = allItems.Except(addedItems).ToList();

        //sorting remainingItems list
        remainingItems = remainingItems.OrderBy(x => x).ToList();

        //adding list of selected items to the FileCabinetsToAdd list
        foreach (var item in addedItems)
        {
                FileCabinetsToAdd.Items.Add(item);
        }

        //clearing FileCabinetsRetreived list
        FileCabinetsRetrieved.Items.Clear();

        //adding remainingItems list to FileCabinetsRetreived list
        foreach (var item in remainingItems)
        {
            FileCabinetsRetrieved.Items.Add(item.ToString());
        }

    }
2
  • 1
    I've readed your code but I'm not completely sure about what are you trying to accomplish. Maybe a UI picture could help me to understand your problem. Just a heads up: you are not sorting FileCabnetsToAdd.Items in your code (in order to do so, you'll need to do something like you are doing with FileCabinetsRetrieved) Commented Jan 16, 2019 at 21:00
  • Imaged added. The listbox on the right is not adding items in alphabetical order, which is what the method should be doing Commented Jan 16, 2019 at 22:35

2 Answers 2

1

Ok, you should sort the Items of the FileCabinetsToAdd Listbox. To do so, you can use the sort property of the listbox:

FileCabinetsToAdd.Sorted = true;

That line should do the work.

If you need a custom sorting, then you should:

  1. back up the items in a list
  2. clear the entire FileCabinetsToAdd.Items collection
  3. add the new items to that list
  4. sort the list
  5. add every item in the list to the FileCabinetsToAdd.Items
Sign up to request clarification or add additional context in comments.

1 Comment

Issue was that I wasn't updating the 2nd listbox the same way. I ended up rewriting the whole method
1

This would be my Add_button click event, although i did not run it so may have some errors, but it's a good start.

List<string> toSort = new List<string>();    

foreach (var item in FileCabinetsRetrieved.SelectedItems)
        {
            FileCabinetsRetrieved.Items.Remove(item);
            toSort.Add(item);
        }
foreach (var item in FileCabinetsToAdd.Items)
        {
            toSort.Add(item);
        }

toSort = toSort.OrderBy(x => x).ToList();
FileCabinetsToAdd.Items.Clear();
foreach (var item in toSort)
{
    FileCabinetsToAdd.Items.Add(item);
}

The important part is clearing the list and then adding all the sorted items again.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.