Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Error in Ruby text comparison?

Reply
Thread Tools

Error in Ruby text comparison?

 
 
Greg Willits
Guest
Posts: n/a
 
      10-27-2007
'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
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Greg Willits
Guest
Posts: n/a
 
      10-27-2007
Greg Willits wrote:
> '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

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

-- gw

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Eustáquio 'TaQ' Rangel
Guest
Posts: n/a
 
      10-27-2007
-----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+puNwOJQB3 yo2gTBHQCfd2Pf
R/rIWKh9b7/tXJphk7KRziI=
=wNTo
-----END PGP SIGNATURE-----

 
Reply With Quote
 
Nobuyoshi Nakada
Guest
Posts: n/a
 
      10-27-2007
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.

--
Nobu Nakada

 
Reply With Quote
 
Wolfgang Nádasi-Donner
Guest
Posts: n/a
 
      10-27-2007
Greg Willits wrote:
> '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
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Devi Web Development
Guest
Posts: n/a
 
      10-27-2007
T24gMTAvMjcvMDcsIFdvbGZnYW5nIE7DoWRhc2ktRG9ubmVyID xlZC5vZGFub3dAd29uYWRvLmRl
PiB3cm90ZToKPiBJZiBZb3Ugd2FudCB0byBjb21wYXJlIFN0cm luZ3MgYmFzZWQgb24gdGhlIHVz
YWdlIGluIGEgbGFuZ3VhZ2UsIHlvdQo+IHNob3VsZCBiZXR0ZX IgdXNlIGFuIGFwcHJvcHJpYXRl
IHNlcXVlbmNlIGRlZmluaXRpb24uCgoKV2hhdCBpcyB0aGUgYm VzdCB3YXkgdG8gY3JlYXRlIGEg
bmV3IHNvcnQgb3JkZXI/CkZvciBleGFtcGxlLCBpZiB0aGUgb3JkZXIgaXMgdGhpczoKJX dbMSAy
IDMgNCA1IDYgNyA4IDkgMCBBIGEgQiBiLi4uXQphbmQgZXZlcn kgb3RoZXIgY2hhcmFjdGVyIHRv
IGJlIHNvcnRlZCB1c2luZyBBcnJheSNzb3J0IGFmdGVyIHRoZQ phbHBoYW51bWVyaWMgY2hhcmFj
dGVycy4KCi0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS 0tLS0tLS0tLS0tLS0KRGFuaWVs
IEJydW1iYXVnaCBLZWVuZXkKRGV2aSBXZWIgRGV2ZWxvcG1lbn QKRGV2aS5XZWJNYXN0ZXJAZ01h
aWwuY29tCi0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS 0tLS0tLS0tLS0tLS0K

 
Reply With Quote
 
Devi Web Development
Guest
Posts: n/a
 
      10-27-2007
On 10/27/07, Devi Web Development <(E-Mail Removed)> wrote:
> 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.

-------------------------------------------
Daniel Brumbaugh Keeney
Devi Web Development
http://www.velocityreviews.com/forums/(E-Mail Removed)
-------------------------------------------

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
#!/usr/bin/ruby , #!/usr/bin/ruby -w , #!/usr/bin/ruby -T?, #!/usr/bin/ruby -T1... anne001 Ruby 1 04-23-2006 03:02 PM
Controlling text in a Text Area or Text leo ASP General 1 12-05-2005 01:13 AM
'text/html' expected 'text/xml' WebService error Saya ASP .Net 0 08-19-2005 11:57 AM
Error: content type of 'text/html; but expected 'text/xml'. ramarnath@gmail.com ASP .Net 1 08-16-2005 09:07 AM
WebService-Error: "the found request-type is 'text/html; charset=utf-8', but 'text/xml' was expected" ASP .Net 0 02-20-2004 03:37 PM



Advertisments