Permalink
Browse files

changed fonts and sizing, new post

  • Loading branch information...
1 parent 1261568 commit 9fd61f2783c4ff3b80b08005d7cfe6be408e10c0 @mhinton committed Apr 2, 2012
View
@@ -20,7 +20,7 @@ p { margin-bottom: 1em; @extend .sans; }
p:first-child { clear: both; margin-top: 1em; }
li {
- font-size: 17px;
+ font-size: 15px;
line-height: 1.5em;
font-family: $sans;
}
@@ -113,7 +113,7 @@ li {
}
p {
- font-size: 17px;
+ font-size: 16px;
line-height: 1.5em;
}
}
@@ -0,0 +1,34 @@
+---
+layout: post
+title: "Ruby Sort a Subset of Array Elements"
+date: 2012-04-02 10:46
+comments: true
+categories: [tech, ruby]
+---
+
+Here is a little trick I came up with for sorting a subset of array elements in
+Ruby.
+
+I needed to sort a subset of elements in an array while leaving the other
+elements in the positions they occupied.
+
+The algorithm I used was to create a new array with just the items I want to
+sort. Sort the new array's elements. Then you have the original array that
+contains all the elements and a second sorted array with just the items you are
+interested in. The original array and the sorted array both contain the same subset
+of items then you want sorted. So go through the original array and whenever you
+find one of the items you wanted to sort replace it with the next item from the
+sorted array.
+
+{% codeblock Ruby array subset sort lang:ruby %}
+ all_elements = [1, 'b', 2, 'a']
+ subset = all_elements.select {|i| String === i}
+ subset.sort!
+ all_elements.map do |i|
+ if i.kind_of?(String)
+ subset.shift
+ else
+ i
+ end
+ end
+{% endcodeblock %}

0 comments on commit 9fd61f2

Please sign in to comment.