Re: [rt-users] Sorting by the value of a custom field in search results *numerically*

2014-05-06 Thread Landon Stewart
Thanks for your replies guys.  And thanks for adding that feature request
Brent (http://issues.bestpractical.com/Ticket/Display.html?id=29638).


On 2 May 2014 13:52, Landon Stewart lstew...@iweb.com 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?

 --
 Landon Stewart :: lstew...@iweb.com
 Lead Specialist, Abuse and Security Management
 Spécialiste principal, gestion des abus et sécurité
 http://iweb.com :: +1 (888) 909-4932





-- 
Landon Stewart :: lstew...@iweb.com
Lead Specialist, Abuse and Security Management
Spécialiste principal, gestion des abus et sécurité
http://iweb.com :: +1 (888) 909-4932
-- 
RT Training - Dallas May 20-21
http://bestpractical.com/training

Re: [rt-users] Sorting by the value of a custom field in search results *numerically*

2014-05-05 Thread Emmanuel Lacour
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:elac...@easter-eggs.com  -   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 000..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

Re: [rt-users] Sorting by the value of a custom field in search results *numerically*

2014-05-05 Thread Emmanuel Lacour
On Mon, May 05, 2014 at 02:21:28PM +0200, Emmanuel Lacour wrote:
 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).
 


I misread your email, my patch just fix sorting on CF edit pages, not in
search results.


-- 
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:elac...@easter-eggs.com  -   http://www.easter-eggs.com
-- 
RT Training - Dallas May 20-21
http://bestpractical.com/training


Re: [rt-users] Sorting by the value of a custom field in search results *numerically*

2014-05-05 Thread Parish, Brent
This is because the custom field values are stored as varchar or text in the 
database.
I added a feature request 
(http://issues.bestpractical.com/Ticket/Display.html?id=29638) for it.

- Brent


-Original Message-
From: rt-users [mailto:rt-users-boun...@lists.bestpractical.com] On Behalf Of 
Emmanuel Lacour
Sent: Monday, May 05, 2014 8:23 AM
To: rt-users@lists.bestpractical.com
Subject: Re: [rt-users] Sorting by the value of a custom field in search 
results *numerically*

On Mon, May 05, 2014 at 02:21:28PM +0200, Emmanuel Lacour wrote:
 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).
 


I misread your email, my patch just fix sorting on CF edit pages, not in search 
results.


-- 
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:elac...@easter-eggs.com  -   http://www.easter-eggs.com
--
RT Training - Dallas May 20-21
http://bestpractical.com/training
-- 
RT Training - Dallas May 20-21
http://bestpractical.com/training