Sorting Query Results both String and Numeric

685
2
04-29-2010 01:08 PM
JamesFaron
Occasional Contributor
I can sort the values of the returned fields in the QueryBuilder Widget from the new Sample Viewer if the value is a string by calling a sortByAttribute function (see code below), and I can sort field if the value is numeric by calling the sortNumeric function (see below). My problem is that I need to be able to do both. I can do one or the other, and I will get a debugging error if, say I use the sortByAttribute function and load values for a number field, and vice versa if I use the sortNumeric function and load values from a string field. I haven't been able to make the script work by calling both functions using various attempts with 'if else'. Any help will be greatly appreciated.
private function onFieldValuesRereivalsuccess(obj:Object):void{
   mqbMessage = "Values has been retreived."
   CursorManager.removeBusyCursor();
   var fset:FeatureSet = obj as FeatureSet;
   mqbMessage = "";
//   mFldValues = fset.features;
   mFldValues2 = fset.features;
   var fldname:String = fieldsLst.selectedItem.Name
   var contains:Boolean;
   mFldValues = new Array();
   for ( var i:Number = 0; i<mFldValues2.length; i++ )
   {
    contains = false;
    for ( var j:Number = 0; j<mFldValues.length; j++ )
    {
     var item1:String = mFldValues2.attributes[fieldsLst.selectedItem.Name];
     var item2:String = mFldValues.attributes[fieldsLst.selectedItem.Name];
     if (item1 == item2) contains = true;
    }
    if (!contains)
    {
     mFldValues.push(mFldValues2);
    }
   }
   mFldValues.sort(sortByAttribute);
  }
  
  //COMPARE FUNCTION
  private function sortByAttribute(a:Object, b:Object):Object
  {
   var x:String = a.attributes[fieldsLst.selectedItem.Name].toLowerCase();
   var y:String = b.attributes[fieldsLst.selectedItem.Name].toLowerCase();
   return ((x < y) ? -1 : ((x > y) ? 1 : 0));
  }
  private function sortNumeric(a:Object, b:Object):int
  {
   var value1:Number = a.attributes[fieldsLst.selectedItem.Name];
   var value2:Number = b.attributes[fieldsLst.selectedItem.Name];
   return ((value1 < value2) ? -1 : ((value1 > value2) ? 1 : 0));
  }


Thanks,

Jim Faron
Austin Independent School District
Austin, TX
Tags (2)
0 Kudos
2 Replies
KenBuja
MVP Esteemed Contributor
I use a sorting function that utilizes the SortField class. This contains a property ("numeric") that allow it to sort using a numeric comparison or a string comparison. If that property is set to null, it inspects the list and selects what comparison to use based on the first item in the list.

public static function sortCollection(arrayColl:ArrayCollection, sortField:String, caseSensistive:Boolean = false, numericSort:Object = null):void
{
   var dataSortField:SortField = new SortField();
   var dataSort:Sort = new Sort();
            
   dataSortField.name = sortField;
   dataSortField.numeric = numericSort;
   dataSortField.caseInsensitive = caseSensistive;
            
   dataSort.fields = [dataSortField];
   arrayColl.sort = dataSort;
   arrayColl.refresh();
}
0 Kudos
JamesFaron
Occasional Contributor
Ken,

Thanks for the response. The QueryBuilderWidget was written with mFldValues as a bindable array, and the adaptation to eliminate the duplicate values, utilizing push() method.  I have tried to wrap the array into an arrayCollection, but in lieu of changing too much code that works, it would be great if there were a way to sort both numbers and text utilizing the compare functions. I may be out of luck here. I will look into modifying the code to work with replacing the Array with an ArrayCollection, but I don't know why the designer of the widget did not do that from the beginning, since it seems that there are better options working with ArrayCollection than Array.

Thanks,
Jim Faron
0 Kudos