Observable Lists, Interfaces, Lists and sorting, oh my...



  • I'm trying to come up with a non-WTF version here but, well.

    Here's the gist:

    I want to display two classes (Reminder and UpcomingDate) in one GridView. They also should be sorted by their DateTime StartDate property.

    So I created an Interface (let's call it IDate) and made the two classes use that interface. Also created an ObservableCollection<IDate>. The instances for the classes are pulled like this:

     List<Reminder> reminders = await _reminderTable
                        .CreateQuery()
                        .ToListAsync();
    

    I could also do a ToCollectionAsync(). Now, here's the thing: Collections<> have no Sort() function. Lists<> do, however.

    So, how do I put the whole sorted shebang into the ObservableCollection<IDate> without resorting to several foreach?



  • I haven't touched C# for a while but I think you can pass the sorted list to the ObservableCollection's constructor.



  • The only idea I'm capable of at this time (it's 01:41 AM CEST here) is deriving ObservableList<T> from LIst<T>, INotifyCollectionChanged, and INotifyPropertyChanged.



  • There's no Sort(), but can't you stick an OrderBy() in there? Mind that AFAIR it's not in-place, though.



  • This is correct. Observable collection is very much a list, and it's constructor won't reorder what you pass in.



  • @coldandtired said:

    I haven't touched C# for a while but I think you can pass the sorted list to the ObservableCollection's constructor.

    Well, that got rid of one foreach. Should've thought of that myself.

    As for the rest - oh well, it's not as if this routine was particularly time sensitive



  • As I recall, there's a class called something like IOrderedEnumerable.

    Or you could sort on display using LINQ.

    Edit: NM - none of my suggestions apply .... I am TR :WTF:


  • Discourse touched me in a no-no place

    @Magus said:

    and it's constructor

    <twitch>



  • <dot tv> WolskSellout WolskSellout


Log in to reply