简体   繁体   中英

Sort dynamic dropdown list alphabetically

I have a dropdown with three strings horizontally for each item. How can I alphabetically sort first by string 1 from each item, then by string 2 from each item?

Here's my snippet:

foreach (var item in list)
{
    if(typeof(T).Equals(typeof(Machine)))
    {
        Machine machine = (item as Machine);
        string title = machine.MachineName + " - " + machine.Serial + " - " + machine.MachineOwnership;
        alert.AddAction(UIAlertAction.Create(title, UIAlertActionStyle.Default, action => {
            button.SetTitle(title, UIControlState.Normal);
        }));
    }
    else if(typeof(T).Equals(typeof(Person)))
    {
        alert.AddAction(UIAlertAction.Create((item as Person).Name, UIAlertActionStyle.Default, action => {
            button.SetTitle((item as Person).Name, UIControlState.Normal);
        }));
    }
}

Where list contains objects of type Machine which has the following properties:

MachineName , Serial and MachineOwnshership (all strings).

So I want to do something like OrderBy(MachineName).ThenBy(Serial) but not sure how to do so correctly when I'm first checking to see what the list type is and then populating the dropdown list per item.

My dropdown list looks something like this if anyone needs clarification:

-------------------------------------------------
MachineNameStartsWithA - 01234 - OwnerStartsWithA
--------------------------------------------------
MachineNameStartsWithB - 012345 - OwnerStartsWithB
---------------------------------------------------

etc.... where it's a long list of items where the strings are separated by "-" like it's shown in the code.

Also, for what it's worth, this is currently inside a Xamarin app.

I think something like this should work. Sorting by a Tuple should be lexicographic:

list.OfType<Machine>().OrderBy(x => new Tuple<string,string>(x.MachineName,x.Serial))

The OfType call should also remove the need for the typeof check and cast - the result should be an iterable of Machine s.


Per the comment regarding multiple object types:

Grouping like types together

This would be my default preference from a UI/UX perspective unless it really makes sense to mix Machine s and Person s together. Depending on the length of the list it may be preferable to split on the element type first as list.OfType will enumerate the entire list each time.

foreach(var item in list.OfType<Machine>().OrderBy(x => new Tuple<string,string>(x.MachineName,x.Serial)))
{
  // append item
}
foreach(var item in list.OfType<Person>().OrderBy(x => x.Name))
{
  // append item
}

Interleaving various types

private Tuple<string,string> Projection(BaseClass x)
{
    Machine item = x as Machine;
    if(item != null)
    {
      return new Tuple<string,string>(item.MachineName,item.Serial);
    }

    Person item = x as Person;
    if(item != null)
    {
      return new Tuple<string,string>(item.Name,"");
    }
}

foreach(var item in list.OrderBy(Projection))
{
  // check type of item and append as appropriate
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM