Making nil comparable.

R

Robert Schaaf

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
 
B

Brian Candler

Robert said:
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?
 
R

Rob Biedenharn

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


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
(e-mail address removed)
 
R

Robert Schaaf

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


Robert said:
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?
 
E

Eric Hodel

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.
 
J

Joel VanderWerf

Robert said:
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"]

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top