Select to view content in your preferred language

Sort Unique Values in Ascending Order using Query Builder

2455
4
05-17-2010 08:17 AM
JasonHancheruk
New Contributor II
I am trying to return only unique values and then sort them in ascending order using Query Builder for SFV1 1.3. I have tried to adapt some codes that were published in the old forums, but nothing has worked. Has anybody implemented this in there own project? Any help would be appreciated.

/*  private function onFieldValuesRereivalsuccess(obj:Object):void{
   mqbMessage = "Values has been retreived."
   CursorManager.removeBusyCursor();
   var fset:FeatureSet = obj as FeatureSet;
   mqbMessage = "";
   mFldValues = fset.features;
  }
*/

  private function onFieldValuesRereivalsuccess(obj:Object):void
  {
   mqbMessage = "Values have been retreived."
   CursorManager.removeBusyCursor();
   var fset:FeatureSet = obj as FeatureSet;
   mqbMessage = "";
//use the uniquearray function to get a unique list.
   mFldValues = uniqueArray(fset.features);
  }

  private function uniqueArray(a:Array) : Array
  {
      var unique : Array = new Array();
      var contains : Boolean;
    
       for( var i:Number = 0; i<a.length; i++ )
      {
            contains = false;
            for (var j:Number = 0; j<unique.length; j++)
           {
                if (a == unique) contains = true;
            }
            if (!contains)
    {
        if (a != "")
        {
            unique.push(a);
        }
    }
      }
      return unique;
  }

Jason
Tags (2)
0 Kudos
4 Replies
CristianJonhson
New Contributor
I dont know if I understood what u were asking for, but maybe it is something like that?
  private function sortArrayRemoveDupl(arr:Array):Array
   {
    var newArr:Array=[];    
    var pivo:Object=arr.shift();   
    arr.sort();    
    newArr.push(pivo);    
    while (pivo!=null)
    {
     if (pivo == arr[0])     
      pivo = arr.shift();
     else{newArr.push(pivo); pivo = arr.shift(); }
    }
    newArr.sort();
    return newArr;
   }


This should set a new array with unique values and order it... but remember, if u give a featureSet to sort and remove duplicated, it will compare FULL objects, not only the field u want, to do a sort on a field from a table, u should use feature.attributes.ATRIBUTE.

Att.,
Cristiano Benato
0 Kudos
SusanMordy1
New Contributor II
Barbara's code posted here: http://forums.esri.com/Thread.asp?c=158&f=2421&t=302204

Will return unique values.

You just need to add:

private var mFldValues2:Array = null;

to QueryBuilderWidget.mxml  in addition to the code provided by Barbara.

Not ascending, but it's a step in the right direction.
0 Kudos
JasonHancheruk
New Contributor II
Sorry I did not reply sooner, but I thought these threads would send an email as the older forums did. Guess I need to check my settings.

The action script I used was incorporated from Mark Hoyland in the forum, http://forums.esri.com/Thread.asp?c=158&f=2421&t=302204, as the one from Barbara Patterson caused 14 errors of "1013: The private attribute may be used only on class definitions" through the rest of the private functions in the QueryBuilderWidget.mxml.

Not sure how to fix this error?

Thanks,
Jason
0 Kudos
NathanEnge
Esri Contributor
private function onFieldValuesRereivalsuccess(obj:Object):void{
   mqbMessage = "Values have been retrieved."
   CursorManager.removeBusyCursor();
   var fset:FeatureSet = obj as FeatureSet;
   mqbMessage = "";
   //First array that contains all the features   
   mFldValues2 = fset.features;
   //The name of the field the user has selected
   var fldname:String = fieldsLst.selectedItem.Name
   var contains : Boolean;
   //The new array that will contain only unique features based on the selected field
   mFldValues = new Array(); 
   
   
   for( var i:Number = 0; i<mFldValues2.length; i++ ) {
            contains = false;
            for (var j:Number = 0; j<mFldValues.length; j++)
           {
              // Lines below return the string value for the selected fields from the 2 arrays
               
               var item1:String = mFldValues2.attributes[fieldsLst.selectedItem.Name];
               var item2:String = mFldValues.attributes[fieldsLst.selectedItem.Name];
               if (item1 == item2) contains = true;
          }
          //if the value is not already in the array add it to the array
            if (!contains)
            {
                 mFldValues.push(mFldValues2);
            }  
      } 
      
      
      var myArray:Array = new Array();
      var strValue:String = "";
      for (var a:Number = 0; a<mFldValues.length; a++)
      { 
       strValue = mFldValues.attributes[fieldsLst.selectedItem.Name].toString();
       myArray.push(strValue);
       //Alert.s...
0 Kudos