Sorting with FQL?

Jul 10, 2007 at 1:11 AM
Possible to sort using Order By in FQL? If not..how do you sort an XML string?
Jul 10, 2007 at 4:41 AM

dmose wrote:
Possible to sort using Order By in FQL? If not..how do you sort an XML string?


how about implementing IComparable to the Entity objects?
Jul 10, 2007 at 7:26 AM
Hi,

FQL cannot be sorted. That may be to speed up the queries, not sure.
What I've done is read the xml string returned by DirectFQLQuery function into a dataset and then created a dataview that can be sorted.

Example of querying a list of friends names and user ids for a given facebook user id.

Dim myFQLstring as string
Dim myXMLString as string

myFQLstring = "SELECT name, uid FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1=" & uid & ")"

myXMLString = DirectFQLQuery(myFQLstring)

Dim MyDataSet As DataSet
Dim myXMLreader As New System.IO.StringReader(myXMLString)
Dim myDataView As DataView

MyDataSet = New DataSet()
MyDataSet.ReadXml(myXMLreader)
myDataView = New DataView(MyDataSet.Tables("user")) 'name of the table from FQL being queried
myDataView.Sort = "name" ' name of the column to be sorted - other example, myDataView.Sort = "name, uid DESC"
myDataList.DataSource = myDataView


Took me a while to get this working as I haven't used XML very much before. So I thought I'd pass it along now that I figured it out.

Hope that helps. Cheers,

Brad
Jul 10, 2007 at 4:26 PM

bflood wrote:
Hi,

FQL cannot be sorted. That may be to speed up the queries, not sure.
What I've done is read the xml string returned by DirectFQLQuery function into a dataset and then created a dataview that can be sorted.

Example of querying a list of friends names and user ids for a given facebook user id.

Dim myFQLstring as string
Dim myXMLString as string

myFQLstring = "SELECT name, uid FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1=" & uid & ")"

myXMLString = DirectFQLQuery(myFQLstring)

Dim MyDataSet As DataSet
Dim myXMLreader As New System.IO.StringReader(myXMLString)
Dim myDataView As DataView

MyDataSet = New DataSet()
MyDataSet.ReadXml(myXMLreader)
myDataView = New DataView(MyDataSet.Tables("user")) 'name of the table from FQL being queried
myDataView.Sort = "name" ' name of the column to be sorted - other example, myDataView.Sort = "name, uid DESC"
myDataList.DataSource = myDataView


Took me a while to get this working as I haven't used XML very much before. So I thought I'd pass it along now that I figured it out.

Hope that helps. Cheers,

Brad



This helps tremendously thanks!.

What I ended up doing was adding IComparable to the Entity's I wanted sorting to wrok on.

For example, the User Entity

I added:

public class User : System.IComparable


and

public int CompareTo(object obj)
{
if(!(obj is User))
throw new InvalidCastException("This object is not of type User");

User u= (User)obj;
return this.Name.CompareTo(u.Name);
}

So when I pull the XML I bind it to entity objects and put them into an ArrayList .. then call sort on the ArrayList with a null parameter which sorts the objects by name.