简体   繁体   中英

How to sort properties of object and populate into an ArrayList

How can I sort the following properties in the class then store in an ArrayList ? I have populated that to an ArrayList but there an issue when page is reloaded items order in the repeater will change. here is population cookies value in to an array

Dim myCookies As HttpCookie=HttpContext.Current.Request.Cookies("Mycard")
Dim varArryItems As ArrayList = New ArrayList

For i AsInteger=0 To varCookies.Values.Count-1
    Dim AllValues As String()=myCookies.Values(i).Split("|"c)
    Dim item As objCard=New objCard

    item.P_ItemID=Integer.Parse(AllValues(0))
    item.P_ItemTitle=AllValues(1).ToString
    item.P_BrandTitle=AllValues(2).ToString
    item.P_ItemImg=AllValues(3).ToString
    item.P_ItemPrice=Decimal.Parse(AllValues(4))
    'item.P_ItemQauntity=Integer.Parse(AllValues(5))
    'item.P_ItemQauntitySelected=Integer.Parse(AllValues(6))
    item.P_BarcodeID=Integer.Parse(AllValues(7))
    item.P_TotalItemPrice=Decimal.Parse(AllValues(8))
    varArryItems.Add(item)
Next

rptcart.DataSource=varArryItems
rptcart.DataBind()

Here is my objCard class store cookies values I need sort all properties I have tried suing ArrayList Sort method it wasn't work for me.

Public Class objCard  
    Private ID As Integer
    Private ItemID As Integer
    Private BarcodeID As Integer
    Private ItemTitle As String
    Private BrandTitle As String
    Private ItemImg As String
    Private ItemPrice As Decimal
    Private TotalItemPrice As String
    Private ItemQauntity As Integer
    Private ItemQauntitySelected As Integer

    Public Property P_ID As Integer
        Get
            Return Me.ID
        End Get
        Set
            Me.ID = Value
        End Set
    End Property

    Public Property P_ItemID As Integer
        Get
            Return Me.ItemID
        End Get
        Set
            Me.ItemID = Value
        End Set
    End Property

    Public Property P_BarcodeID As Integer
        Get
            Return Me.BarcodeID
        End Get
        Set
            Me.BarcodeID = Value
        End Set
    End Property

    Public Property P_ItemTitle As String
        Get
            Return Me.ItemTitle
        End Get
        Set
            Me.ItemTitle = Value
        End Set
    End Property

    Public Property P_BrandTitle As String
        Get
            Return Me.BrandTitle
        End Get
        Set
            Me.BrandTitle = Value
        End Set
    End Property

    Public Property P_ItemImg As String
        Get
            Return Me.ItemImg
        End Get
        Set
            Me.ItemImg = Value
        End Set
    End Property

    Public Property P_ItemPrice As Decimal
        Get
            Return Me.ItemPrice
        End Get
        Set
            Me.ItemPrice = Value
        End Set
    End Property

    Public Property P_TotalItemPrice As String
        Get
            Return Me.TotalItemPrice
        End Get
        Set
            Me.TotalItemPrice = Value
        End Set
    End Property

    Public Property P_ItemQauntity As Integer
        Get
            Return Me.ItemQauntity
        End Get
        Set
            Me.ItemQauntity = Value
        End Set
    End Property

    Public Property P_ItemQauntitySelected As Integer
        Get
            Return Me.ItemQauntitySelected
        End Get
        Set
            Me.ItemQauntitySelected = Value
        End Set
    End Property
End Class

If you have

Public Class Card  
    Property ID As Integer
    Property ItemID As Integer
    Property BarcodeID As Integer
    Property ItemTitle As String
    Property BrandTitle As String
    Property ItemImg As String
    Property ItemPrice As Decimal
    Property TotalItemPrice As Decimal
    Property ItemQuantity As Integer
    Property ItemQuantitySelected As Integer

End Class

Then you can use a List(Of Card) to store the data. This has the advantange that the compiler knows that it has instanced of Card in it instead of just some object.

Dim myCookies As HttpCookie = HttpContext.Current.Request.Cookies("Mycard")
Dim cards = New List(Of Card)

For i As Integer = 0 To varCookies.Values.Count-1
    Dim allValues As String() = myCookies.Values(i).Split("|"c)
    Dim item = New Card

    item.ItemID = Integer.Parse(allValues(0))
    item.ItemTitle = allValues(1).ToString
    item.BrandTitle = allValues(2).ToString
    item.ItemImg = allValues(3).ToString
    item.ItemPrice = Decimal.Parse(allValues(4))
    'item.ItemQuantity = Integer.Parse(allValues(5))
    'item.ItemQuantitySelected = Integer.Parse(allValues(6))
    item.BarcodeID = Integer.Parse(allValues(7))
    item.TotalItemPrice = Decimal.Parse(allValues(8))
    cards.Add(item)

Next

And now that the compiler can get to the properties of the entries in the list, you can

Dim dataToPresent = cards.OrderBy(function(c) c.ItemId).ToList()
rptcart.DataSource = dataToPresent
rptcart.DataBind()

and it will show the data in the order you chose.

If you need to order by different properties at run-time then a search for "linq dynamic orderby" should give you useful code.

I noticed that you had Private TotalItemPrice As String which conflicted with item.P_TotalItemPrice=Decimal.Parse(AllValues(8)) . If you use Option Strict On then Visual Studio will point out problems like that for you.

PS You have Dim myCookies but you use varCookies.Values.Count . You may want to check that that is correct.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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