如何排序列表?

时间:2013-01-01 17:59:49

标签: asp.net vb.net list

我试图以多种方式对列表进行排序,但没有一个对我有用。我一定做错了什么。我想对列表details进行排序,然后将其序列化并将其发送到UI,以便我在UI​​中有一个排序列表。 所以基本上我希望Return strJson返回排序(按排序属性排序)列表。希望我有意义。

<WebMethod(Description:="Get Home Page Items Page Wise", EnableSession:=True)> _
    <ScriptMethod(ResponseFormat:=ResponseFormat.Json)>
    Public Function GetHomePageItemsPageWise(ByVal pageIndex As String) As Object
        Dim details As New List(Of HomePageObject)()

        Dim idObject As New List(Of GetIdBasedOnInterest)()
        idObject = CType(BLL.GetDataByInterests(CType(BLL.GetAccIdFromSocialAuthSession(), Integer)), List(Of GetIdBasedOnInterest))


        Dim cmd As DbCommand = _db.GetStoredProcCommand("GetHomePageObjectPageWise")
        _db.AddInParameter(cmd, "PageIndex", SqlDbType.VarChar, pageIndex)
        _db.AddInParameter(cmd, "PageSize", SqlDbType.Int, 10)
        _db.AddOutParameter(cmd, "PageCount", SqlDbType.Int, 1)
        _db.AddInParameter(cmd, "whereStoryID", SqlDbType.VarChar, idObject(0).StoryIds)
        _db.AddInParameter(cmd, "whereAlbumID", SqlDbType.VarChar, idObject(0).AlbumIds)
        _db.AddInParameter(cmd, "wherePictureID", SqlDbType.VarChar, idObject(0).PictureIds)

        Try
            Using ds As DataSet = _db.ExecuteDataSet(cmd)
                For Each rs As DataRow In ds.Tables(0).Rows
                    Dim homePageObject As New HomePageObject()

                    homePageObject.AlbumId = rs("AlbumId").ToString()
                    homePageObject.StoryTitle = rs("StoryTitle").ToString()
                    homePageObject.AlbumName = rs("AlbumName").ToString()
                    homePageObject.AlbumCover = rs("AlbumCover").ToString()
                    homePageObject.Votes = rs("Votes").ToString()
                    homePageObject.PictureId = rs("PictureId").ToString()
                    homePageObject.TableName = rs("tableName").ToString()
                    homePageObject.PageCount = CType(cmd.Parameters("@PageCount").Value, Integer)
                    homePageObject.Sort = Guid.NewGuid()
                    details.Add(homePageObject)
                    Next
            End Using
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

        Dim js As New JavaScriptSerializer()
        Dim strJson As String = js.Serialize(details.ToArray)
        Return strJson
    End Function

1 个答案:

答案 0 :(得分:1)

要随机化列表,您可以执行以下操作。 (并且您不需要Sort中的HomePageObject属性来完成此操作)

Dim rnd As new Random()
Dim strJson As String = js.Serialize(details.OrderBy(Function(x) rnd.Next()).ToArray())