On Fri, May 02, 2014 at 01:52:06PM -0700, Landon Stewart wrote:
>    Hello,
>    It seems that the sorting by custom field in search results is always done
>    alphabetically.  
>    For example sorting by CF that contains only integers in descending order:
>    4
>    3
>    2
>    11
>    1
>    Is there a way to sort search results by custom field numerically?
>


I use the attached patch for 4.2.3 (not very efficient, but it works).


-- 
Easter-eggs                              Spécialiste GNU/Linux
44-46 rue de l'Ouest  -  75014 Paris  -  France -  Métro Gaité
Phone: +33 (0) 1 43 35 00 37    -   Fax: +33 (0) 1 43 35 00 76
mailto:[email protected]  -   http://www.easter-eggs.com
diff --git a/rt/local/lib/RT/CustomFieldValues_Local.pm b/rt/local/lib/RT/CustomFieldValues_Local.pm
new file mode 100644
index 0000000..d167f18
--- /dev/null
+++ b/rt/local/lib/RT/CustomFieldValues_Local.pm
@@ -0,0 +1,45 @@
+package RT::CustomFieldValues;
+
+use strict;
+no warnings qw(redefine);
+
+
+=head2 Array
+
+Return the values as an array
+
+=cut
+
+sub Array {
+    my $self = shift;
+
+    my @values;
+    while (my $value = $self->Next) {
+        push @values, $value;
+    }
+
+    return @values;
+
+}
+
+=head2 SortedArray
+
+Return the values as an array sorted by Sortorder, then by name using natural sort
+
+=cut
+
+sub SortedArray {
+    my $self = shift;
+
+    my @values = $self->Array;
+
+    use Sort::Key;
+    use Sort::Key::Natural qw(natkeysort);
+
+    my @sorted = natkeysort { $_->Category." ".$_->SortOrder." ".$_->Name } @values;
+    
+    return @sorted;
+    
+}
+
+1;
diff --git a/rt/share/html/Elements/EditCustomFieldSelect b/rt/share/html/Elements/EditCustomFieldSelect
index 222fcd9..e8548d8 100644
--- a/rt/share/html/Elements/EditCustomFieldSelect
+++ b/rt/share/html/Elements/EditCustomFieldSelect
@@ -110,7 +110,8 @@ jQuery(  function () {
   </div>
 %   }
 %   my $CFVs = $CustomField->Values;
-%   while ( my $value = $CFVs->Next ) {
+%   my @sorted_values = $CFVs->SortedArray;
+%   foreach my $value ( @sorted_values ) {
 %     my $content = $value->Name;
 %     my $labelid = "$name-". $value->id;
 <div data-name="<% $value->Category || '' %>">
@@ -186,9 +187,10 @@ $MaxValues => 1
 % $_ = lc $_ foreach @Default;
 % my $selected;
 % my $CFVs = $CustomField->Values;
+% my @sorted_values = $CFVs->SortedArray;
 % my @levels;
-% while ( my $value = $CFVs->Next ) {
+% foreach my $value ( @sorted_values ) {
 %       my $name = $value->Name;
 %       my $category = $value->Category || '';
 %       my $level = (split /:/, $category, 2)[0] || '';
 %       while (@levels) {
-- 
RT Training - Dallas May 20-21
http://bestpractical.com/training

Reply via email to