-1

Brand new to C#, this is so confusing and my code is probably not as neat as some would want. Bear with me I'm learning.

I have a list of 'Items' called 'shoppingList'.

When the 'alphabetical' button is clicked, I want to rearrange the items in 'shoppingList' by 'Name' that is in the 'Item' class. Once the list is rearranged, the list is redrawn on the form.

I know there may be a solution on here already, but after trying so many of them I just believe my knowledge is not enough. Please use baby speak with me if you still know it. Happy to provide any more info also. Thank you in advance

public partial class Form1:Form{
    List<Item> shoppingList = new List<Item>() { };
}

public class Item{
    public Item(string Name, int Amount)
{
        this.name = Name;
        this.amount = Amount;

    }

    public string getname() { return name; }
    public int getamount() { return amount; }

    public int amount;
    public string name;

}

private void alphabetical_Click(object sender, EventArgs e)
{
    //What to put here?
    ProcessAndDrawShoppingList();
}
Luke
  • 43
  • 4
  • 1
    Just create a class implementing `IComparer` for each type of comparison you want (name, amount, etc). Then you can call `this.shoppingList.Sort(new ItemComparerByName());`. Note that if it's only for presentation purposes you might want to keep a _view_ of the list where you apply sorting (asc/desc), filtering and whatever else your UI might require, for example paging). – Adriano Repetti May 04 '20 at 08:31
  • 1
    I´m pretty sure there arose plenty of similar questions on Stack when you typed your question. I can´t believe you didn´t find anything useful in them. – MakePeaceGreatAgain May 04 '20 at 08:36
  • Actually not alphabetically. My code is also slightly different to everyone elses, therefore I can't quite follow what they're doing. If this was some other language that would be fine, but my amateur skills with C# are shining. – Luke May 04 '20 at 08:45

1 Answers1

2

Can you not just use the .OrderBy() function?

So in the function alphabetical_Click have something like:

 var sortedList = shoppingList.OrderBy(x => x.Name);  // Guessing that this is the list but I don't know how you have access to it.

OR just set the original list to the value of the sorted list:

shoppinglist = shoppingList.OrderBy(x => x.Name).ToList();

Alternatively you could use the .OrderByDescending() to order the opposite way.

I don't know what the list is in this case but it looks like you are using Xamarin/ Xamarin.Forms so just get the list from that and use the OrderBy() function

JamesS
  • 2,111
  • 1
  • 9
  • 25
  • I dont know if i did it correctly, but I've followed a few examples on OrderBy() and although it's compiled, the list still draws in the order it was created. – Luke May 04 '20 at 08:36
  • 1
    Note that `OrderBy` doesn't sort the original list, it gives you a new sorted list. – DavidG May 04 '20 at 08:37
  • 1
    You should assign the result of `OrderBy` to some variable, as the function won´t **modifiy** your list, but just **create** a new one. – MakePeaceGreatAgain May 04 '20 at 08:37
  • @LukeHowes Sorry, `OrderBy()` returns a new sorted list. I have edited the answer to show how you can order `shoppingList` to show this. You can either create a new list or set shoppingList to the value of the orderly – JamesS May 04 '20 at 08:40
  • @JamesS `OrderBy` will give you an `IEnumerable` not a `List` so you can't just assign it like that. – DavidG May 04 '20 at 08:40
  • Many thanks, this works now. To think I have spent hours today trying to solve this and it was two lines of code, my gosh. Thanks again – Luke May 04 '20 at 08:49
  • @LukeHowes No problem. Be sure to mark as correct for future users. – JamesS May 04 '20 at 08:54