Forum: Ruby Making nil comparable.

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
6e191124d011c102f295ae1bccf20ee1?d=identicon&s=25 Robert Schaaf (Guest)
on 2009-06-05 21:57
(Received via mailing list)
I have a array of foo objects, each of which contains an array bar
values.  I'd like to sort the foos, ordered by the bars.  The problem
is that the bars are of unequal length.

The most apt example is the numbering in an outline: 1, 1.1, 1.2,
1.2.1, 1.3 ...etc.

In practical terms, comparing 1.1 to 1.2.1 would be [1, 1, nil] <=>
[1, 2, 1].  Array#sort_by barfs on the nil.  Is there some nice way to
let nil always sort low?

Also having problems with a Regexp, but I can't find the appropriate
forum to cry in.

Cheers,

Bob Schaaf
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2009-06-05 22:05
Robert Schaaf wrote:
> I have a array of foo objects, each of which contains an array bar
> values.  I'd like to sort the foos, ordered by the bars.  The problem
> is that the bars are of unequal length.
>
> The most apt example is the numbering in an outline: 1, 1.1, 1.2,
> 1.2.1, 1.3 ...etc.
>
> In practical terms, comparing 1.1 to 1.2.1 would be [1, 1, nil] <=>
> [1, 2, 1].  Array#sort_by barfs on the nil.  Is there some nice way to
> let nil always sort low?

irb(main):002:0> [1,1] <=> [1,2,1]
=> -1

So would

    sort_by { |x| x.bars.compact }

be sufficient?
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2009-06-05 22:15
(Received via mailing list)
On Jun 5, 2009, at 3:56 PM, Robert Schaaf wrote:
>
> Also having problems with a Regexp, but I can't find the appropriate
> forum to cry in.
>
> Cheers,
>
> Bob Schaaf


Why make the arrays equal length with nils?

%w[ 1.3 1.1 1.2.1 1.2 1 ].sort_by{|outline| outline.split('.').map{|n|
n.to_i} }
=> ["1", "1.1", "1.2", "1.2.1", "1.3"]

We can handle Regexp questions here, too... but you have to toss in a
bit of ruby to keep it fair. ;-)

-Rob

Rob Biedenharn    http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
6e191124d011c102f295ae1bccf20ee1?d=identicon&s=25 Robert Schaaf (Guest)
on 2009-06-05 23:37
(Received via mailing list)
Thanks Brian and Rob,

Yes, of course. Compact will work because the nils are always
trailing.  Thanks greatly.

I should have RTFB.

Now I know this is only peripherally topical, but can anyone come up
with the regexp that achieves this:

string =~ pattern
[$1, $2, $3]

  where string is a Roman numeral, optionall followed by "(?)" or
alpha, which, if present can be followed by an Arabic number,

produces

I -> ["I", nil, nil]
I(?) -> ["I", "(?)", nil]
Ia -> ["I", "a", nil]
Ib -> ["I", "b", nil]
IIa1 -> ["II", "a", "1"]
IIa2 -> ["II", "a", "2"]

As for the trailing nils, I didn't want to deal with empty strings,
but since now I'm compacting, I can use reject! instead.

I've got Dave Burt's RomanNumerals module, and it was easy to make his
regexp capturing (and it will be stored as Arabic.)

/^(M*(?:D?C{0,3}|C[DM])(?:L?X{0,3}|X[LC])(?:V?I{0,3}|I[VX]))$/i

Now that the Ruby stuff is out of the way, who wants to put me out of
my misery?  It's not like I'm asking you to paint my house!

With interim gratitude,

Bob Schaaf
58479f76374a3ba3c69b9804163f39f4?d=identicon&s=25 Eric Hodel (Guest)
on 2009-06-06 00:19
(Received via mailing list)
On Jun 5, 2009, at 12:56, Robert Schaaf wrote:

> I have a array of foo objects, each of which contains an array bar
> values.  I'd like to sort the foos, ordered by the bars.  The
> problem is that the bars are of unequal length.
>
> The most apt example is the numbering in an outline: 1, 1.1, 1.2,
> 1.2.1, 1.3 ...etc.
>
> In practical terms, comparing 1.1 to 1.2.1 would be [1, 1, nil] <=>
> [1, 2, 1].  Array#sort_by barfs on the nil.  Is there some nice way
> to let nil always sort low?

Don't add nil:

irb(main):003:0> [[1, 1], [1], [1, 1, 2], [2], [1, 2, 1], [1, 3]].sort
=> [[1], [1, 1], [1, 1, 2], [1, 2, 1], [1, 3], [2]]

If you have nil, use #compact on each Array you're sorting first.
47b1910084592eb77a032bc7d8d1a84e?d=identicon&s=25 Joel VanderWerf (Guest)
on 2009-06-06 00:40
(Received via mailing list)
Robert Schaaf wrote:
>
> I -> ["I", nil, nil]
> I(?) -> ["I", "(?)", nil]
> Ia -> ["I", "a", nil]
> Ib -> ["I", "b", nil]
> IIa1 -> ["II", "a", "1"]
> IIa2 -> ["II", "a", "2"]

Do you need the regex to validate the Roman numeral itself? If not, the
following passes your tests (assuming that you don't mind compacting out
the nils). You can do validation subsequently.

rx = /\A([IVXLCDM]+)(?:(\(\?\))|([a-z])(\d+)?)?\z/

strs = [
   ["I",    ["I", nil, nil]],
   ["I(?)", ["I", "(?)", nil]],
   ["Ia",   ["I", "a", nil]],
   ["Ib",   ["I", "b", nil]],
   ["IIa1", ["II", "a", "1"]],
   ["IIa2", ["II", "a", "2"]]
]

strs.each do |str, expected|
   if rx.match str
     match = $~.captures
     if match.compact == expected.compact
       puts "#{str.inspect} ok"
     else
       puts "#{str.inspect} fails: match=#{match.inspect}"
     end
   else
     puts "#{str.inspect} fails: not matched"
   end
end
This topic is locked and can not be replied to.