Link to home
Start Free TrialLog in
Avatar of vdurbal
vdurbalFlag for United States of America

asked on

How to sort a list of objects using an item in an enclosed list

Hello,

Let's say I have the following objects:

Public Class clsCarLot

  Public Owner As clsOwner
  Public Property Location As String
  Public Property Cars As List (of clsCar)

End Class


Public Class clsCar

  Public Property Make As String
  Public Property Model As String
  Public Property Year As Integer
  Public Property Price As Single
  Public Property Owners As List (Of clsOwner)
  Public Property Features As List (Of clsCarFeature)

End Class


Public Class clsOwner

  Public Property LastName As String
  Public Property FirstName As String
  Public Property PurchaseDate As Date
  Public Property SellDate As Date

End Class


Public Class clsCarFeature
  
  Public Property Name As String
  Public Property Price As Single

End Class

Open in new window


One important point is that "Features" will be a user-defined list.  For instance a particular car lot owner might want to keep track of whether the cars have tinted windows, and sunroofs.  While another might care about anti-lock brakes, GPS, etc.  But all of the cars in the car lot will have the same list of features, though the cost of a particular feature will vary from car to car.

So let's assume I create an object of clsCarLot called "UsedCarLot", define the owner of the lot and its location, then add in a bunch of car objects (of class clsCar), with each car object defined by its Make, Model, Year, Price, a list of its current and previous owners, and a list of whatever features that car might have (e.g. tinted windows costing $500, sunroof costing $1000, etc.).  

My question is this, how can I implement multi-level sorting on the "Cars" property of the UsedCarLot object using both hard-coded and user-defined properties.  For example, let's say I wanted to create a new list of clsCar, sorted by Price, Make, Model and Feature Price where Feature Name = "GPS".  How would I do that?

I have seen examples where LINQ is used to sort on actual properties of an object (e.g. "Make" or "Model" of clsCar), but I cannot find any examples using a property defined at runtime.  Is there any way to do a sort like in my example above?  I am currently using VB.NET 2010.  Thank you.

Vijay
Avatar of Ark
Ark
Flag of Russian Federation image

Dim cars As New List(Of clsCar)
Dim selectedCars = cars.Where(Function(x) x.Features.Any(Function(y) y.Name = "sunroof" OrElse y.Name = "GPS"))

Open in new window

PS. add checking for x.Futures IsNot Nothing to avoid errors or initialize lists prior:
Public Property Features As New List(Of clsCarFeature)
With OrderBy
Dim cars As New List(Of clsCar)
Dim selectedCars = cars.Where(Function(x) x.Features.Any(
                   Function(y) y.Name = "sunroof" OrElse
                               y.Name = "GPS")).
                       OrderBy(Function(x) x.Make).
                       ThenBy(Function(x) x.Model)

Open in new window

Avatar of vdurbal

ASKER

Hello Ark,

thank you for your response.  I feel I am almost there.  In your answer, it looks like query you've created will find all of the cars with a sunroof or GPS, then order it by Make, then by Model.  However, assuming all of the cars in the lot will have a feature with Name = "GPS", how would I adjust the query to sort the list by the price of the GPS feature in call of the cars, then by the Make and Model?

I've tried the following to only sort by GPS price:

Dim selectedCars = cars.OrderBy(Function(x) x.Features.Any(Function(y) y.Price)).Where(Function(x) x.Features.Any(Function(y) y.Name = "GPS"))

I figured that if that worked, I could add ".OrderBy(Function(x) x.Make).ThenBy(Function(x) x.Model) after it to do the secondary/tertiary sorts, but it doesn't seem to work.  What am I doing wrong?  Thank you.
ASKER CERTIFIED SOLUTION
Avatar of Ark
Ark
Flag of Russian Federation image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of vdurbal

ASKER

Thanks, Ark.  This helps.