Select to view content in your preferred language

Query Widget 3.0 sort results

3029
13
Jump to solution
09-27-2012 05:25 AM
NedCake
Deactivated User
There were code samples available at previous versions of SFV to enable sorting in this widget. Nothing yet at version 3.0. Any help would be appreciated.

Ned Cake
TLCGIS
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
NedCake
Deactivated User
We got it to work! This will sort using the title field designated in the config file.

Add This function to QueryWidget .mxml

                        //COMPARE FUNCTION
   private function sortByAttribute(a:Object, b:Object):Object
   {
    var x:String = a.attributes[queryTitleField].toLowerCase();
    var y:String = b.attributes[queryTitleField].toLowerCase();
    return ((x < y) ? -1 : ((x > y) ? 1 : 0));
   }

Add this code to the query features function at about line 310
                       // sort feature set
        featureSet.features.sort(sortByAttribute); //sort by calling compare function


Thanks to John Marquez from TLCGIS and ESRI staff
Ned Cake

View solution in original post

0 Kudos
13 Replies
MarcWeinshenker1
Regular Contributor
I'm struggling with this myself as I am converting my Flex viewer apps from 2.5 to 3.0.  I've tried a lot of things and nothing works.  I'm going to file a support incident and see if I can get esri help.  Really can't understand why sorting is not including in the published widget.

[edit -- incident has been assigned.  I will share whatever comes of it.]

Marc
0 Kudos
NedCake
Deactivated User
We got it to work! This will sort using the title field designated in the config file.

Add This function to QueryWidget .mxml

                        //COMPARE FUNCTION
   private function sortByAttribute(a:Object, b:Object):Object
   {
    var x:String = a.attributes[queryTitleField].toLowerCase();
    var y:String = b.attributes[queryTitleField].toLowerCase();
    return ((x < y) ? -1 : ((x > y) ? 1 : 0));
   }

Add this code to the query features function at about line 310
                       // sort feature set
        featureSet.features.sort(sortByAttribute); //sort by calling compare function


Thanks to John Marquez from TLCGIS and ESRI staff
Ned Cake
0 Kudos
MarcWeinshenker1
Regular Contributor
Outstanding!  Thank you.
0 Kudos
BrianKratcha
Regular Contributor
Thanks I have it sorting in most cases,

One problem that I ran into is that the code gives an error when the sort field values start with numbers.

For example = 139-Newton or 155- Thomas.  Unfortunately, I cannot change the values of the data.

Any ideas on this?
0 Kudos
BjornSvensson
Esri Regular Contributor
FYI: In the next version (3.1) of the Flex Viewer, you will be able to specify a sort field (orderbyfield) for both Query and Search widget.
0 Kudos
PeterHoffman
Deactivated User
I am having trouble modifying the code, where do I put the featureSet. stuff? Can you show a section of the code because I am getting a 1069 error.

Thanks,
0 Kudos
MarcWeinshenker1
Regular Contributor
Here you go.

                    function onResult(featureSet:FeatureSet, token:Object = null):void
                    {
                        try
                        {
    // sort feature set with sortByAttribute compare function added below
    featureSet.features.sort(sortByAttribute); 
       
                            queryResultAC = createQueryResults(featureSet);
                            addSharedData(widgetTitle, queryResultAC);

                            clearMessage();


I am having trouble modifying the code, where do I put the featureSet. stuff? Can you show a section of the code because I am getting a 1069 error.

Thanks,
0 Kudos
PeterHoffman
Deactivated User
I made the code changes, now I get a error 1009. If I comment out the sort line I get 691 records but they are not sorted of course.
0 Kudos
MarcWeinshenker1
Regular Contributor
Not sure what to suggest, but make sure there are no obvious problems with the added function, such as missing some content or that it was copied into the wrong place.  Maybe even delete the function then copy and paste it back in from the code posted above.  Look at the configuration xml to be sure the layer, the query, and the fields are properly accounted for and maybe experiment with other layers/fields to see if they behave differently.  Have you made other modifications to the Query widget code or just this?  Also, what Flex SDK are you using?  I'm using 4.6.   If none of these things turns up anything, then it's beyond my expertise.

I made the code changes, now I get a error 1009. If I comment out the sort line I get 691 records but they are not sorted of course.
0 Kudos