locked
Sort a Collection of Custom Objects RRS feed

  • Question

  • Hello,

    I have a class called Product with the following elements:

    Public pName As String
    Public pPrice As Double

    I want to order the items in a collection of this class by their price, I tried this code :

    Dim vTemp As Product
    Dim vItm As Product
    Dim Products As Collection
    
    For i = 1 To Products.Count - 1
            For j = i + 1 To Products.Count
                If Losers(i).pPrice> Losers(j).pPrice Then
                    'store the lesser item
                    'Set vTemp = New Product
                    Set vTemp = Products(j)
                    'remove the lesser item
                    Products.Remove j
                    're-add the lesser item before the
                    'greater Item
                    Products.Add vTemp
                    'Set vTemp = Nothing
                End If
            Next j
        Next i

    But the list is still not sorted  after this piece of code. I also tried a Heap Sort but it does not ork on a colelction of a custom class. 

    Thank you. 

    Edit : I would like to implemet this on an excel macro


    • Edited by Lel14 Sunday, September 27, 2015 6:00 PM
    • Moved by KareninstructorMVP Sunday, September 27, 2015 11:35 PM Moved from VB.NET
    Sunday, September 27, 2015 5:39 PM

Answers

  • Hi,

    There is no such method we can sort the collection object. We can achieve the goal by two ways.

    The first is create a new collection object and when we add the item to the collection object, we can compare the item in collection with the new item to be added. Then add the item to new collection based on the order we wanted.

    The second is that, we write an order algorithm for ourselves to order the collection object.

    Hope it is helpful.

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, September 29, 2015 1:57 AM

All replies

  • Lel,

    I would set this whole thing up differently, but to answer your question, you can use LINQ-To-Object for this:

    Option Strict On Option Explicit On Option Infer Off Public Class Form1 Private Sub Form1_Load(sender As System.Object, _ e As System.EventArgs) _ Handles MyBase.Load End Sub Private Sub Example() Dim pList As New List(Of Products) With pList .Add(New Products With {.pName = "Something", .pPrice = 1.59}) .Add(New Products With {.pName = "Something Else", .pPrice = 3}) End With Dim qry As System.Linq.IOrderedEnumerable(Of Products) = _ From p As Products In pList _ Order By p.pPrice Stop End Sub End Class Public Class Products Public pName As String Public pPrice As Double End Class

    ***** EDIT *****

    I forgot to call it from the form's .Load event. ;-)

    Obviously you'll want to add that.


    "I never failed once... It just happened to be a 2000-step process." -- Thomas Edison


    Sunday, September 27, 2015 5:55 PM
  • I'm sorry that I forgot to mention this, but does it work on Excel macro?
    Sunday, September 27, 2015 5:59 PM
  • I'm sorry that I forgot to mention this, but does it work on Excel macro?

    No, you must be in the wrong forum.

    This one is for Visual Basic (dotNet), not Excel.


    "I never failed once... It just happened to be a 2000-step process." -- Thomas Edison

    Sunday, September 27, 2015 6:03 PM
  • ok but since Excel uses vba i thought it was the appropriate forum. Is there a way to implement this code in excel?
    Sunday, September 27, 2015 6:06 PM
  • ok but since Excel uses vba i thought it was the appropriate forum. Is there a way to implement this code in excel?

    I can only say that I don't know (because I don't know VBA).

    VBA is similar, but it's not at all the same.

    *****

    If you have experience in VBA though, you might want to consider a move to a stand-alone application written in VB Net - it's a step in the right direction in my opinion.

    For what it's worth. :)


    "I never failed once... It just happened to be a 2000-step process." -- Thomas Edison

    Sunday, September 27, 2015 6:08 PM
  • ok but since Excel uses vba i thought it was the appropriate forum. Is there a way to implement this code in excel?

    https://social.msdn.microsoft.com/Forums/en-US/home?forum=isvvba

    Sunday, September 27, 2015 7:25 PM
  • Hi,

    There is no such method we can sort the collection object. We can achieve the goal by two ways.

    The first is create a new collection object and when we add the item to the collection object, we can compare the item in collection with the new item to be added. Then add the item to new collection based on the order we wanted.

    The second is that, we write an order algorithm for ourselves to order the collection object.

    Hope it is helpful.

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, September 29, 2015 1:57 AM