<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Sorting Query Results both String and Numeric in ArcGIS API for Flex Questions</title>
    <link>https://community.esri.com/t5/arcgis-api-for-flex-questions/sorting-query-results-both-string-and-numeric/m-p/251274#M5995</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;private function onFieldValuesRereivalsuccess(obj:Object):void{
&amp;nbsp;&amp;nbsp; mqbMessage = "Values has been retreived."
&amp;nbsp;&amp;nbsp; CursorManager.removeBusyCursor();
&amp;nbsp;&amp;nbsp; var fset:FeatureSet = obj as FeatureSet;
&amp;nbsp;&amp;nbsp; mqbMessage = "";
//&amp;nbsp;&amp;nbsp; mFldValues = fset.features;
&amp;nbsp;&amp;nbsp; mFldValues2 = fset.features;
&amp;nbsp;&amp;nbsp; var fldname:String = fieldsLst.selectedItem.Name
&amp;nbsp;&amp;nbsp; var contains:Boolean;
&amp;nbsp;&amp;nbsp; mFldValues = new Array();
&amp;nbsp;&amp;nbsp; for ( var i:Number = 0; i&amp;lt;mFldValues2.length; i++ )
&amp;nbsp;&amp;nbsp; {
&amp;nbsp;&amp;nbsp;&amp;nbsp; contains = false;
&amp;nbsp;&amp;nbsp;&amp;nbsp; for ( var j:Number = 0; j&amp;lt;mFldValues.length; j++ )
&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; var item1:String = mFldValues2&lt;I&gt;.attributes[fieldsLst.selectedItem.Name];
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; var item2:String = mFldValues&lt;J&gt;.attributes[fieldsLst.selectedItem.Name];
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (item1 == item2) contains = true;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&amp;nbsp;&amp;nbsp;&amp;nbsp; if (!contains)
&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mFldValues.push(mFldValues2&lt;I&gt;);
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&amp;nbsp;&amp;nbsp; }
&amp;nbsp;&amp;nbsp; mFldValues.sort(sortByAttribute);
&amp;nbsp; }
&amp;nbsp; 
&amp;nbsp; //COMPARE FUNCTION
&amp;nbsp; private function sortByAttribute(a:Object, b:Object):Object
&amp;nbsp; {
&amp;nbsp;&amp;nbsp; var x:String = a.attributes[fieldsLst.selectedItem.Name].toLowerCase();
&amp;nbsp;&amp;nbsp; var y:String = b.attributes[fieldsLst.selectedItem.Name].toLowerCase();
&amp;nbsp;&amp;nbsp; return ((x &amp;lt; y) ? -1 : ((x &amp;gt; y) ? 1 : 0));
&amp;nbsp; }
&amp;nbsp; private function sortNumeric(a:Object, b:Object):int
&amp;nbsp; {
&amp;nbsp;&amp;nbsp; var value1:Number = a.attributes[fieldsLst.selectedItem.Name];
&amp;nbsp;&amp;nbsp; var value2:Number = b.attributes[fieldsLst.selectedItem.Name];
&amp;nbsp;&amp;nbsp; return ((value1 &amp;lt; value2) ? -1 : ((value1 &amp;gt; value2) ? 1 : 0));
&amp;nbsp; }&lt;/I&gt;&lt;/J&gt;&lt;/I&gt;&lt;/PRE&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Jim Faron&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Austin Independent School District&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Austin, TX&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 29 Apr 2010 20:08:37 GMT</pubDate>
    <dc:creator>JamesFaron</dc:creator>
    <dc:date>2010-04-29T20:08:37Z</dc:date>
    <item>
      <title>Sorting Query Results both String and Numeric</title>
      <link>https://community.esri.com/t5/arcgis-api-for-flex-questions/sorting-query-results-both-string-and-numeric/m-p/251274#M5995</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;private function onFieldValuesRereivalsuccess(obj:Object):void{
&amp;nbsp;&amp;nbsp; mqbMessage = "Values has been retreived."
&amp;nbsp;&amp;nbsp; CursorManager.removeBusyCursor();
&amp;nbsp;&amp;nbsp; var fset:FeatureSet = obj as FeatureSet;
&amp;nbsp;&amp;nbsp; mqbMessage = "";
//&amp;nbsp;&amp;nbsp; mFldValues = fset.features;
&amp;nbsp;&amp;nbsp; mFldValues2 = fset.features;
&amp;nbsp;&amp;nbsp; var fldname:String = fieldsLst.selectedItem.Name
&amp;nbsp;&amp;nbsp; var contains:Boolean;
&amp;nbsp;&amp;nbsp; mFldValues = new Array();
&amp;nbsp;&amp;nbsp; for ( var i:Number = 0; i&amp;lt;mFldValues2.length; i++ )
&amp;nbsp;&amp;nbsp; {
&amp;nbsp;&amp;nbsp;&amp;nbsp; contains = false;
&amp;nbsp;&amp;nbsp;&amp;nbsp; for ( var j:Number = 0; j&amp;lt;mFldValues.length; j++ )
&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; var item1:String = mFldValues2&lt;I&gt;.attributes[fieldsLst.selectedItem.Name];
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; var item2:String = mFldValues&lt;J&gt;.attributes[fieldsLst.selectedItem.Name];
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (item1 == item2) contains = true;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&amp;nbsp;&amp;nbsp;&amp;nbsp; if (!contains)
&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mFldValues.push(mFldValues2&lt;I&gt;);
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&amp;nbsp;&amp;nbsp; }
&amp;nbsp;&amp;nbsp; mFldValues.sort(sortByAttribute);
&amp;nbsp; }
&amp;nbsp; 
&amp;nbsp; //COMPARE FUNCTION
&amp;nbsp; private function sortByAttribute(a:Object, b:Object):Object
&amp;nbsp; {
&amp;nbsp;&amp;nbsp; var x:String = a.attributes[fieldsLst.selectedItem.Name].toLowerCase();
&amp;nbsp;&amp;nbsp; var y:String = b.attributes[fieldsLst.selectedItem.Name].toLowerCase();
&amp;nbsp;&amp;nbsp; return ((x &amp;lt; y) ? -1 : ((x &amp;gt; y) ? 1 : 0));
&amp;nbsp; }
&amp;nbsp; private function sortNumeric(a:Object, b:Object):int
&amp;nbsp; {
&amp;nbsp;&amp;nbsp; var value1:Number = a.attributes[fieldsLst.selectedItem.Name];
&amp;nbsp;&amp;nbsp; var value2:Number = b.attributes[fieldsLst.selectedItem.Name];
&amp;nbsp;&amp;nbsp; return ((value1 &amp;lt; value2) ? -1 : ((value1 &amp;gt; value2) ? 1 : 0));
&amp;nbsp; }&lt;/I&gt;&lt;/J&gt;&lt;/I&gt;&lt;/PRE&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Jim Faron&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Austin Independent School District&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Austin, TX&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 29 Apr 2010 20:08:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-flex-questions/sorting-query-results-both-string-and-numeric/m-p/251274#M5995</guid>
      <dc:creator>JamesFaron</dc:creator>
      <dc:date>2010-04-29T20:08:37Z</dc:date>
    </item>
    <item>
      <title>Re: Sorting Query Results both String and Numeric</title>
      <link>https://community.esri.com/t5/arcgis-api-for-flex-questions/sorting-query-results-both-string-and-numeric/m-p/251275#M5996</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
public static function sortCollection(arrayColl:ArrayCollection, sortField:String, caseSensistive:Boolean = false, numericSort:Object = null):void
{
&amp;nbsp;&amp;nbsp; var dataSortField:SortField = new SortField();
&amp;nbsp;&amp;nbsp; var dataSort:Sort = new Sort();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp; dataSortField.name = sortField;
&amp;nbsp;&amp;nbsp; dataSortField.numeric = numericSort;
&amp;nbsp;&amp;nbsp; dataSortField.caseInsensitive = caseSensistive;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp; dataSort.fields = [dataSortField];
&amp;nbsp;&amp;nbsp; arrayColl.sort = dataSort;
&amp;nbsp;&amp;nbsp; arrayColl.refresh();
}
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 12:28:06 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-flex-questions/sorting-query-results-both-string-and-numeric/m-p/251275#M5996</guid>
      <dc:creator>KenBuja</dc:creator>
      <dc:date>2021-12-11T12:28:06Z</dc:date>
    </item>
    <item>
      <title>Re: Sorting Query Results both String and Numeric</title>
      <link>https://community.esri.com/t5/arcgis-api-for-flex-questions/sorting-query-results-both-string-and-numeric/m-p/251276#M5997</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Ken,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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.&amp;nbsp; 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.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Jim Faron&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 30 Apr 2010 16:03:19 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-flex-questions/sorting-query-results-both-string-and-numeric/m-p/251276#M5997</guid>
      <dc:creator>JamesFaron</dc:creator>
      <dc:date>2010-04-30T16:03:19Z</dc:date>
    </item>
  </channel>
</rss>

