Error in Ruby text comparison?

G

Greg Willits

'1sqHmb5b8G9mN' < '1Xv5LeB9bMdar'

Wouldn't you think that is supposed to be TRUE ?

All my text editors and Excel and Numbers all sort it so that 1s...
comes before 1X...

But Ruby says the above comparison is false.

What am I missing?

-- gw
 
G

Greg Willits

Greg said:
'1sqHmb5b8G9mN' < '1Xv5LeB9bMdar'

Wouldn't you think that is supposed to be TRUE ?

All my text editors and Excel and Numbers all sort it so that 1s...
comes before 1X...

But Ruby says the above comparison is false.

What am I missing?

Strange, this list:

s, S, s, a, B

in Excel, Numbers, and in Araelium Edit comes out as this when sorted

a, B, s, S, s

TextWrangler puts them as

a, B, s, s, S

Ruby sorts ['s','S','s','a','B'].sort as

["B", "S", "a", "s", "s"]

MySQL (as I have it set up) sorts like the OS X apps.

Trying to do a binary search with a ruby array based on text sorted by
something else is getting hosed.

Oh... duh, have Ruby sort it :p

Could get very expensive, but I guess I'll have to do it.

-- gw
 
E

Eustáquio 'TaQ' Rangel

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi!
Ruby's sorting these strings by ASCII order, and as you can see here, capital letters come first! So "A" is always less than 'a', etc.

This is because Ruby follow the lexicographical order for sorting. If you need
case-insensitive comparisons, you can change the way the sorting works with:

puts '1sqHmb5b8G9mN' < '1Xv5LeB9bMdar'
puts 'a' < 'B'

class String
alias <=> casecmp
end

puts '1sqHmb5b8G9mN' < '1Xv5LeB9bMdar'
puts 'a' < 'B'

Let's see what ri tell us about casecmp:

ri casecmp
- --------------------------------------------------------- String#casecmp
str.casecmp(other_str) => -1, 0, +1
- ------------------------------------------------------------------------
Case-insensitive version of String#<=>.

"abcdef".casecmp("abcde") #=> 1
"aBcDeF".casecmp("abcdef") #=> 0
"abcdef".casecmp("abcdefg") #=> -1
"abcdef".casecmp("ABCDEF") #=> 0

Best regards,

- --
Eustáquio "TaQ" Rangel
http://eustaquiorangel.com

"When someone says, 'I want a programming language in which I need only say what
I want done,' give him a lollipop."
Alan Perlis


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)

iD8DBQFHIzTYb6UiZnhJiLsRApzlAKCPKoMhI2Wt+puNwOJQB3yo2gTBHQCfd2Pf
R/rIWKh9b7/tXJphk7KRziI=
=wNTo
-----END PGP SIGNATURE-----
 
N

Nobuyoshi Nakada

Hi,

At Sat, 27 Oct 2007 20:13:28 +0900,
Greg Willits wrote in [ruby-talk:276099]:
'1sqHmb5b8G9mN' < '1Xv5LeB9bMdar'

Wouldn't you think that is supposed to be TRUE ?
No.

But Ruby says the above comparison is false.

'1sqHmb5b8G9mN'.casecmp('1Xv5LeB9bMdar') < 0

returns true.
 
W

Wolfgang Nádasi-Donner

Greg said:
'1sqHmb5b8G9mN' < '1Xv5LeB9bMdar'

Wouldn't you think that is supposed to be TRUE ?

All my text editors and Excel and Numbers all sort it so that 1s...
comes before 1X...

But Ruby says the above comparison is false.

What am I missing?

-- gw

Ruby - like several other languages - makes the comparison based on the
underlying code. When you only have English texts it may be confusing,
because you expect a different behavior.

I think it is always a bad idea to compare texts in this way, because
the character sequence is first based on language definitions (e.g.
letter "ö" is in German the same lake "oe", while in Swedish it comes
after "z"), and second for a language there may be different standards
too (e.g. telefone book sequence versus language definition sequence, as
in German).

If You want to compare Strings based on the usage in a language, you
should better use an appropriate sequence definition.

Wolfgang Nádasi-Donner
 
D

Devi Web Development

T24gMTAvMjcvMDcsIFdvbGZnYW5nIE7DoWRhc2ktRG9ubmVyIDxlZC5vZGFub3dAd29uYWRvLmRl
PiB3cm90ZToKPiBJZiBZb3Ugd2FudCB0byBjb21wYXJlIFN0cmluZ3MgYmFzZWQgb24gdGhlIHVz
YWdlIGluIGEgbGFuZ3VhZ2UsIHlvdQo+IHNob3VsZCBiZXR0ZXIgdXNlIGFuIGFwcHJvcHJpYXRl
IHNlcXVlbmNlIGRlZmluaXRpb24uCgoKV2hhdCBpcyB0aGUgYmVzdCB3YXkgdG8gY3JlYXRlIGEg
bmV3IHNvcnQgb3JkZXI/CkZvciBleGFtcGxlLCBpZiB0aGUgb3JkZXIgaXMgdGhpczoKJXdbMSAy
IDMgNCA1IDYgNyA4IDkgMCBBIGEgQiBiLi4uXQphbmQgZXZlcnkgb3RoZXIgY2hhcmFjdGVyIHRv
IGJlIHNvcnRlZCB1c2luZyBBcnJheSNzb3J0IGFmdGVyIHRoZQphbHBoYW51bWVyaWMgY2hhcmFj
dGVycy4KCi0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0KRGFuaWVs
IEJydW1iYXVnaCBLZWVuZXkKRGV2aSBXZWIgRGV2ZWxvcG1lbnQKRGV2aS5XZWJNYXN0ZXJAZ01h
aWwuY29tCi0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0K
 
D

Devi Web Development

For example, if the order is this:

Mind you, that was just a sample, I really want to understand the best
method to make any arbitrary sort order.
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top