         (@CategoryID is null or cat.CategoryID=@CategoryID) and
         c.IsDeleted = 0
         and	c.TopicMovedID is null	
-
-      select @PageIndex = @PageIndex+1;
-      select @firstselectrownum = (@PageIndex - 1) * @PageSize + 1 
-        -- find first selectedrowid 
-   if (@firstselectrownum > 0)   
-   set rowcount @firstselectrownum
-   else
-   -- should not be 0
-   set rowcount 1
-    
-   select		
-        @firstselectposted = c.LastPosted		
-    from
-            [{databaseOwner}].[{objectQualifier}Topic] c
-        join [{databaseOwner}].[{objectQualifier}User] b on b.UserID=c.UserID
-        join [{databaseOwner}].[{objectQualifier}Forum] d on d.ForumID=c.ForumID
+	
+	select @PageIndex = @PageIndex+1;
+	select @FirstSelectRowNumber = (@PageIndex - 1) * @PageSize + 1;
+	select @LastSelectRowNumber = (@PageIndex - 1) * @PageSize + @PageSize;
+	
+	with TopicIds  as
+	 (
+	 select ROW_NUMBER() over (order by cat.SortOrder asc, d.SortOrder asc, c.LastPosted desc) as RowNum, c.TopicID
+	 from  [{databaseOwner}].[{objectQualifier}Topic] c
+	 	join [{databaseOwner}].[{objectQualifier}Forum] d on d.ForumID=c.ForumID
         join [{databaseOwner}].[{objectQualifier}ActiveAccess] x  with(nolock) on x.ForumID=d.ForumID
         join [{databaseOwner}].[{objectQualifier}Category] cat on cat.CategoryID=d.CategoryID
-    where
+	 where
         (c.LastPosted between @SinceDate and @ToDate) and
         x.UserID = @PageUserID and
         CONVERT(int,x.ReadAccess) <> 0 and

)
AS
begin
   declare @post_totalrowsnumber int 
   declare @firstselectrownum int   
   declare @firstselectposted datetime
  -- declare @ceiling decimal  
  -- declare @offset int 

    set nocount on	

    -- find total returned count
        select
        @post_totalrowsnumber = count(1)		
        from
        [{databaseOwner}].[{objectQualifier}Topic] c
        join [{databaseOwner}].[{objectQualifier}User] b on b.UserID=c.UserID

             when 1 then
               (SELECT top 1 LastAccessDate FROM [{databaseOwner}].[{objectQualifier}TopicReadTracking] y WHERE y.TopicID=c.TopicID AND y.UserID = @PageUserID)
             else ''	 end,
        TotalRows = @TotalRows,
        PageIndex = @PageIndex
    from
	    TopicIds ti
        inner join [{databaseOwner}].[{objectQualifier}Topic] c on c.TopicID = ti.TopicID
        join [{databaseOwner}].[{objectQualifier}User] b on b.UserID=c.UserID
        join [{databaseOwner}].[{objectQualifier}Forum] d on d.ForumID=c.ForumID	  
        join [{databaseOwner}].[{objectQualifier}Category] cat on cat.CategoryID=d.CategoryID
    where ti.RowNum between @FirstSelectRowNumber and @LastSelectRowNumber
		order by
            RowNum ASC   	
    
end
GO
