简体   繁体   English

按字母顺序对动态下拉列表排序

[英]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? 我怎样才能按字母顺序首先从每个项目的字符串1排序,然后再从每个项目的字符串2排序?

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: 其中list包含类型为Machine对象,该对象具有以下属性:

MachineName , Serial and MachineOwnshership (all strings). MachineNameSerialMachineOwnshership (所有字符串)。

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. 因此,我想执行类似OrderBy(MachineName).ThenBy(Serial)但是在我先检查列表类型是什么,然后填充每个项目的下拉列表时,不确定如何正确执行操作。

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. 此外,它的价值在于,它当前在Xamarin应用程序内部。

I think something like this should work. 我认为这样的事情应该起作用。 Sorting by a Tuple should be lexicographic: Tuple排序应按字典顺序:

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. OfType调用还应该消除对typeof检查和typeof的需要-结果应该是Machine的可迭代项。


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. 从UI / UX角度来看,这将是我的默认首选项,除非将MachinePerson混合在一起确实很有意义。 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. 根据list的长度,最好先将元素类型拆分为list.OfType都会枚举整个列表。

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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