Select to view content in your preferred language

Query Widget - Results sorted Alphabetically or Numerically

3226
19
10-07-2010 11:57 AM
BrianKratcha
Regular Contributor
Is there an easy way to have my Query Widget return results sorted alphabetically or numerically?  Right now the results come back sorted randomly. 

Thanks,

Brian
Tags (2)
0 Kudos
19 Replies
MarcWeinshenker1
Regular Contributor
Here's a solution I picked up off the forums a while back.  You'll need to edit QueryWidget.mxml and recompile.  In the function onResult, add the middle line as shown in the first code snippet to call a sorting function for the query results.

 
                            queryResultAC = createQueryResults(featureSet);
       queryResultAC = SortDataGridData("title",queryResultAC);
                            addSharedData(widgetTitle, queryResultAC);


Then put this sorting function somewhere in the module.
               
private function SortDataGridData(strFieldNameToSort:String,recACToSort:ArrayCollection):ArrayCollection{  
    try{
     // CC -Orginal code by BJ and found at forum post http://forums.esri.com/Thread.asp?c=158&f=2421&t=285419&mc=6#msgid886159
     var dataSortField:SortField = new SortField();
     dataSortField.name=strFieldNameToSort;
     var alphabetDataSort:Sort = new Sort();
     alphabetDataSort.fields=[dataSortField];
     recACToSort.sort=alphabetDataSort;
     recACToSort.refresh();
    }
    catch(error:Error){
    }
    return recACToSort;
   } 
0 Kudos
BrianKratcha
Regular Contributor
Here's a solution I picked up off the forums a while back.  You'll need to edit QueryWidget.mxml and recompile.  In the function onResult, add the middle line as shown in the first code snippet to call a sorting function for the query results.

 
                            queryResultAC = createQueryResults(featureSet);
       queryResultAC = SortDataGridData("title",queryResultAC);
                            addSharedData(widgetTitle, queryResultAC);


Then put this sorting function somewhere in the module.
               
private function SortDataGridData(strFieldNameToSort:String,recACToSort:ArrayCollection):ArrayCollection{  
    try{
     // CC -Orginal code by BJ and found at forum post http://forums.esri.com/Thread.asp?c=158&f=2421&t=285419&mc=6#msgid886159
     var dataSortField:SortField = new SortField();
     dataSortField.name=strFieldNameToSort;
     var alphabetDataSort:Sort = new Sort();
     alphabetDataSort.fields=[dataSortField];
     recACToSort.sort=alphabetDataSort;
     recACToSort.refresh();
    }
    catch(error:Error){
    }
    return recACToSort;
   } 



Thanks for the reply RockvilleGIS,

One thing I had to add was this code below into the QueryWidget.mxml code to keep from getting an error.


                                      import mx.collections.SortField;
   import mx.collections.Sort;
   import mx.collections.ArrayCollection;


Thanks again,

Brian
0 Kudos
R__CraigHolmes
Emerging Contributor
NICE!
Thanks Gents.
I wasn't putting in the   import mx.collections.Sort;    along with the  import mx.collections.SortField;
She's sortin' now though!
0 Kudos
TorstenSchramm
Regular Contributor
great work
thanks,
Torsten
0 Kudos
JuneAcosta
Frequent Contributor
I just added the above code to the query widget for Flex Viewer 2.2, but my data is still not sorting. I changed the "title" to the field name of my layer that I want to sort-- I get no errors, but still no sorting. Did anyone else run into this issue? Did you resolve it?

-June
0 Kudos
KeithBornhorst
Deactivated User
June,

Change the sort field back to "title" and the Query widget will sort your results based upon the <titlefield> specified in your widget config file. The embedded code only allows sorting on QueryResult fields (title, content, link, etc.). Additional, more complex code would be needed to support sorting on
alternative fields.
0 Kudos
DerekTokarz1
Emerging Contributor
I keep getting an error in Flex builder that says

"1013: The private attribute may only be used on class property definitions."
The error refers to the Array Collection field or line shown in the code below.

// for Alpha
     private function SortDataGridData(strFieldNameToSort:String,recACToSort:ArrayCollection):ArrayCollection{ 
      try{
       // CC -Orginal code by BJ and found at forum post http://forums.esri.com/Thread.asp?c=158&f=2421&t=285419&mc=6#msgid886159
       var dataSortField:SortField = new SortField();
       dataSortField.name=strFieldNameToSort;
       var alphabetDataSort:Sort = new Sort();
       alphabetDataSort.fields=[dataSortField];
       recACToSort.sort=alphabetDataSort;
       recACToSort.refresh();
      }
      catch(error:Error){
      }
      return recACToSort;
     }

Any ideas as to what can be causing this. I am a beginner in flex so please bare with me.
Thanks,

Derek
0 Kudos
JuneAcosta
Frequent Contributor
Keith,

Thanks for the help- I would have never figured it out.

-june
0 Kudos
SarahSchrader
Occasional Contributor
Thank you!  This worked perfectly!!  The fact that our query list was not alphabetical was one of the first things my supervisor noticed and asked to have changed.  I'm glad that you already put this code together and shared it so I didn't have to figure out how to create this from scratch.
0 Kudos