diff options
author | Fabrice Bellingard <bellingard@gmail.com> | 2012-03-09 17:12:28 +0100 |
---|---|---|
committer | Fabrice Bellingard <bellingard@gmail.com> | 2012-03-09 17:12:28 +0100 |
commit | 4544580152dd4079ae011885e083e93ab8341aed (patch) | |
tree | fe395277cd706eaecef4ed88b6e9e35b39074387 | |
parent | db81905fafe6e0ac098d793769ea77b42267aa66 (diff) | |
download | sonarqube-4544580152dd4079ae011885e083e93ab8341aed.tar.gz sonarqube-4544580152dd4079ae011885e083e93ab8341aed.zip |
SONAR-3294 Elements of "History table" widget is unordered
=> They are now ordered using the natural "human" order
-rw-r--r-- | sonar-server/src/main/webapp/WEB-INF/app/helpers/application_helper.rb | 56 | ||||
-rw-r--r-- | sonar-server/src/main/webapp/WEB-INF/app/views/dashboard/_widget_properties.html.erb | 2 |
2 files changed, 57 insertions, 1 deletions
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</a></span>" 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 @@ <div id="error<%= widget.id -%>" class="error" style="display: none"></div> <table class="table width100"> <tbody> - <% 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| %> <tr> <td class="form-key-cell"><%= property_def.key() -%><%= "*" unless property_def.optional()==true -%></td> <td class="form-val-cell" id="row_<%= property_def.key() -%>"> |