From 4544580152dd4079ae011885e083e93ab8341aed Mon Sep 17 00:00:00 2001 From: Fabrice Bellingard Date: Fri, 9 Mar 2012 17:12:28 +0100 Subject: [PATCH] SONAR-3294 Elements of "History table" widget is unordered => They are now ordered using the natural "human" order --- .../WEB-INF/app/helpers/application_helper.rb | 56 +++++++++++++++++++ .../dashboard/_widget_properties.html.erb | 2 +- 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/sonar-server/src/main/webapp/WEB-INF/app/helpers/application_helper.rb b/sonar-server/src/main/webapp/WEB-INF/app/helpers/application_helper.rb index 2197090c2c0..17b0894601b 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/helpers/application_helper.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/helpers/application_helper.rb @@ -616,5 +616,61 @@ module ApplicationHelper html += message('reviews.remove_this_filter') html += "\">X" end + + # + # Compares 2 strings using the human natural order. + # For instance, the following array: + # ['foo10', 'foo2', foo1'] + # , will be sorted this way: + # ['foo1', 'foo2', foo10'] + # , instead of (with standard String sort): + # ['foo1', 'foo10', foo2'] + # + # Source: http://www.justskins.com/forums/natural-order-sort-101199.html + # + def natural_comparison(str1, str2, caseInsensitive=false) + str1, str2 = str1.dup, str2.dup + compareExpression = /^(\D*)(\d*)(.*)$/ + + if caseInsensitive + str1.downcase! + str2.downcase! + end + + # Remove all whitespace + str1.gsub!(/\s*/, '') + str2.gsub!(/\s*/, '') + + while (str1.length > 0) or (str2.length > 0) do + # Extract non-digits, digits and rest of string + str1 =~ compareExpression + chars1, num1, str1 = $1.dup, $2.dup, $3.dup + + str2 =~ compareExpression + chars2, num2, str2 = $1.dup, $2.dup, $3.dup + + # Compare the non-digits + case (chars1 <=> chars2) + when 0 + # Non-digits are the same, compare the digits... + # If either number begins with a zero, then compare + # alphabetically, otherwise compare numerically + if (num1[0] != 48) and (num2[0] != 48) + num1, num2 = num1.to_i, num2.to_i + end + + case (num1 <=> num2) + when -1 then return -1 + when 1 then return 1 + end + when -1 then return -1 + when 1 then return 1 + end # case + + end # while + + # Strings are naturally equal + return 0 + end end diff --git a/sonar-server/src/main/webapp/WEB-INF/app/views/dashboard/_widget_properties.html.erb b/sonar-server/src/main/webapp/WEB-INF/app/views/dashboard/_widget_properties.html.erb index 4432a095d6f..abcd69ca28d 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/views/dashboard/_widget_properties.html.erb +++ b/sonar-server/src/main/webapp/WEB-INF/app/views/dashboard/_widget_properties.html.erb @@ -5,7 +5,7 @@ - <% widget.java_definition.getWidgetProperties().each do |property_def| %> + <% widget.java_definition.getWidgetProperties().sort {|w1, w2| natural_comparison(w1.key, w2.key) }.each do |property_def| %>
<%= property_def.key() -%><%= "*" unless property_def.optional()==true -%> -- 2.39.5