Re: Can't Figure Out How To Sort On Bind

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Why don't u try to sort the arraylist before binding ?
using something like this :

public class MySearchResults
{
public MySearchResults(string aa, string bb, string cc) { }

public class Comparer : System.Collections.IComparer
{
public int Compare(object x, object y)
{
MySearchResultsx1 = x as MySearchResults;
MySearchResultsy1 = y as MySearchResults;

if (x1 == null || y1 == null)
throw new ArgumentException("Wrong type");
else
return string.Compare(x1.aa, y1.bb);
}
}

cheers
Andrew

"Fernando Rodriguez, MCP" wrote:

You need to get a book about coding basics.

The sort order is not being passed as an argument, it is being concatenated
with the string "searchCache" to create a key for it, so the cache should
countain a copy of the ArrayList with the requested sorting, you're missing
the piece of code where the ArrayList is actually sorted and stored in the
cache.

By the way, storing different copies of the same data but with different
sorting is a very bad practice. I would just store one copy and then sort it
before binding by using the ArrayList's Sort method.

HTH,



"pbd22" <dushkin@xxxxxxxxx> wrote in message
news:c2c51608-7824-4cdd-88d0-fee40dc3a932@xxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Hi.

I am returning to an old bit of code in our program and need to figure
out how to sort
my columns on bind. I am sorting on Date (mostly) and some other
values.
Problem is, the code is an ArrayList that seems to do some
tricky stuff with a chache object and I am unable to get any sort of
sorting happening.

I have provided the method below (and some supporting methods). The
interesting bit
of code (to me) is:

'Assign ColumnOrder to ViewState
ViewState("SortOrder") = ColumnOrder
'Set up Cache Object and determine if it exists
searchCache = CType(Cache.Get("searchCache" &
ColumnOrder), ArrayList)

It looks like the ArrayList is stored in Cache and then when a new
search parameter is
requested, the cache is called and the new parameter is passed as an
argument?

Any help in explaining how to attack this would be very helpful. I am
not too familiar with
doing the sorting on bind and can't do this in the stored procedure.

Thanks!

-------------------------------------------------------------------------------------------------------------------------------------------------------------

Sub GetDataReader(ByVal ColumnOrder As String)

Try
'Assign ColumnOrder to ViewState
ViewState("SortOrder") = ColumnOrder
'Set up Cache Object and determine if it exists
searchCache = CType(Cache.Get("searchCache" &
ColumnOrder), ArrayList)

'TEMP
searchCache = Nothing

If (searchCache Is Nothing) Then

'Dim sqlStr As String = "call sp_myContent;"

Dim sqlStr As String = "call sp_SearchContent;"

Dim strConn As String =
"SERVER=localhost;DATABASE=MCS;" & _
"UID=vbuser;PASSWORD=vbuser06492;"
Dim MyConnection As New MySqlConnection(strConn)
MyConnection.Open()
Dim MyCommand As New MySqlCommand(sqlStr,
MyConnection)
Dim objDataReader As MySqlDataReader =
MyCommand.ExecuteReader() 'command.close removed
'Create instances of the class,
Dim bkResults As New ArrayList()
'Loop through DataReader
While objDataReader.Read()
With bkResults
If Not objDataReader.IsDBNull(0) Then
.Add(New
MySearchResults(objDataReader.GetString(0).Replace("<u>/",
"<u>").Remove(objDataReader.GetString(0).LastIndexOf("__"),
objDataReader.GetString(0).Length).ToString(), _

objDataReader.GetString(1).ToString(),
objDataReader.GetString(2).ToString(),
objDataReader.GetString(3).ToString(),
objDataReader.GetString(4).ToString(),
objDataReader.GetString(4).ToString()))
End If
End With
End While



'Insert ArrayList into Cache Object with unique
identifier
Cache.Insert("searchCache" & ColumnOrder,
bkResults)
'Close DataReader Connection
objDataReader.Close()
'Bind DataGrid from ArrayList
MyDataGrid.DataSource = bkResults

Else

'Bind DataGrid from Cached ArrayList
MyDataGrid.DataSource = searchCache

End If

MyDataGrid.DataBind()

'Clear ArrayList
bkResults = Nothing

Catch ex As Exception
myLog.WriteEntry(ex.Message,
EventLogEntryType.Warning)
End Try

End Sub

Sub MyDataGrid_Page(ByVal sender As Object, ByVal e As
DataGridPageChangedEventArgs)
Try
MyDataGrid.CurrentPageIndex = e.NewPageIndex
GetDataReader(ViewState("SortOrder").ToString())
Catch ex As Exception
myLog.WriteEntry(ex.Message,
EventLogEntryType.Warning)
End Try
End Sub


Sub MyDataGrid_Sort(ByVal sender As Object, ByVal e As
DataGridSortCommandEventArgs)
Try
MyDataGrid.CurrentPageIndex = 0

GetDataReader(SortOrder(e.SortExpression.ToString()))
Catch ex As Exception
myLog.WriteEntry(ex.Message,
EventLogEntryType.Warning)
End Try

End Sub

Function SortOrder(ByVal Field As String) As String

Try
If Field = ViewState("SortOrder").ToString() Then
SortOrder = Replace(Field, "asc", "desc")
Else
SortOrder = Replace(Field, "desc", "asc")
End If

Catch ex As Exception
myLog.WriteEntry(ex.Message,
EventLogEntryType.Warning)
End Try

End Function



.



Relevant Pages

  • Re: Cant Figure Out How To Sort On Bind
    ... The sort order is not being passed as an argument, ... with the string "searchCache" to create a key for it, ... 'Assign ColumnOrder to ViewState ... Sub GetDataReader(ByVal ColumnOrder As String) ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: VB6 LISTBOX problem
    ... Have a look at the code below, can you do a sort that is faster ... There are of course all sorts of ways of sorting stuff, especially stuf that is "linked together" as in your UDTs, but to prove the following code sorts it in exactly the same way that your own original code does so, by concatenating all three items of each element into a composite string exactly as you are already doing, but instead of dumping those composite strings into a ListBox it dumps them into a VB string array. ... Private Type SAFEARRAY1D ... Dim StPtr As Long, VAs String, pVAs Long ...
    (microsoft.public.vb.general.discussion)
  • Re: need help sorting text by trailing Numbers Value
    ... You can try the following VBA code and see if it works to give you a sort string you can use. ... Dim strReturn As String ... Then in the next step how could i move all of the trailing Numerical charachters to a new field? ...
    (microsoft.public.access.reports)
  • Re: Sort Type
    ... Like I said, the Space$ function itself it is about 50 per cent faster than the equivalent String$ function, but the main speed improvement will come from minimizing the number of times you call your Normalize function by taking it out of the sort routine itself. ... I can't really comment further on your normalize function since you have not provided a comprehensive sample of the stuff you intend to sort but, whatever function you end up using, my own previous suggestion of normalizing your strings into a temporary array just once and then performing your ShuttleMerge Index Sort on the temporary array will still produce a massive speed increase regardless of what you end up with in your Normalize function. ...
    (microsoft.public.vb.general.discussion)
  • C Sharp sorting considered superior to C by an order of magnitude
    ... following sort algorithms and the following points. ... its own comparision for the types bool, byte, short, int, long, ... single, double and string. ... private CheckBox CHKdeterminism; ...
    (comp.programming)