From 7e454cf3255351b1a80cff7a00168a0bc6abf547 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Tue, 27 Mar 2012 14:58:09 +0200 Subject: [PATCH] Add ruby methods Api::Utils#insensitive_sort(arr) and Api::Utils#insensitive_sort!(arr) --- .../webapp/WEB-INF/app/models/api/utils.rb | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/sonar-server/src/main/webapp/WEB-INF/app/models/api/utils.rb b/sonar-server/src/main/webapp/WEB-INF/app/models/api/utils.rb index 6d89067a591..e8a6b4038a0 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/models/api/utils.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/models/api/utils.rb @@ -77,4 +77,51 @@ class Api::Utils end result end + + # Returns a new array created by sorting arr + # Since Sonar 2.15 + # + # Examples : + # Api::Utils.insensitive_sort(['foo', 'bar']) + # Api::Utils.insensitive_sort([foo, bar]) { |elt| elt.nullable_field_to_compare } + # + def self.insensitive_sort(arr) + if block_given? + arr.sort do |a, b| + a_string=yield(a) || '' + b_string=yield(b) || '' + a_string.downcase <=> b_string.downcase || a_string <=> b_string + end + else + arr.sort do |a, b| + a_string=a || '' + b_string=b || '' + a_string.downcase <=> b_string.downcase || a_string <=> b_string + end + end + end + + + # Sorts arr + # Since Sonar 2.15 + # + # Examples : + # Api::Utils.insensitive_sort!(['foo', 'bar']) + # Api::Utils.insensitive_sort!([foo, bar]) { |elt| elt.nullable_field_to_compare } + # + def self.insensitive_sort!(arr) + if block_given? + arr.sort! do |a, b| + a_string=yield(a) || '' + b_string=yield(b) || '' + a_string.downcase <=> b_string.downcase || a_string <=> b_string + end + else + arr.sort! do |a, b| + a_string=a || '' + b_string=b || '' + a_string.downcase <=> b_string.downcase || a_string <=> b_string + end + end + end end -- 2.39.5