UWP Visual Studio 2017 ObservableCollection 排序

UWP Visual Studio 2017 ObservableCollection sorting

我在尝试找到一种对可观察集合进行排序的方法时遇到了一些麻烦。我目前正在尝试通过使用按钮中的事件来测试此过程,该按钮被按下以实时更改显示可观察集合的列表视图,我知道我不能使用 "Sort" 命令,但我能够完成此操作通过使用 "OrderBy" 命令。我目前的代码如下:

public sealed partial class MainPage : Page
{

    ObservableCollection<DataType> collection = new ObservableCollection<DataType>();

    public MainPage()
    {
        this.InitializeComponent();

        setupCollection();
    }

    public void setupCollection()
    {
        collection.Add(new DataType { times = "08:30" });
        collection.Add(new DataType { times = "00:30" });
        collection.Add(new DataType { times = "12:30" });
        collection.Add(new DataType { times = "23:30" });
        collection.Add(new DataType { times = "18:30" });
        collection.Add(new DataType { times = "15:30" });
        collection.Add(new DataType { times = "06:30" });
        collection.Add(new DataType { times = "05:30" });
        collection.Add(new DataType { times = "14:00" });
        collection.Add(new DataType { times = "12:00" });
        listview.ItemsSource = collection;
    }

    public class DataType
    {
        public string times { get; set; }
    }

    private void Button_Tapped(object sender, TappedRoutedEventArgs e)
    {
        collection = new ObservableCollection<DataType>(from i in DataType orderby i.times select i);
        //collection.OrderBy(i.DataType > i.times);
    }
}

有谁知道修复我的代码以便我可以在其中订购商品的方法吗?

问题是:您正在 class 数据类型中搜索,您需要在集合中搜索,以便您可以获取值并将它们按顺序排列...这是解决方案:

collection = new ObservableCollection<DataType>(
    from i in collection orderby i.times select i);

这对我有用。 也希望你。

简单易行的方法:

ConceptItems = new ObservableCollection<DataConcept>(ConceptItems.OrderBy(i => i.DateColumn));