Error in Ruby text comparison?

Discussion in 'Ruby' started by Greg Willits, Oct 27, 2007.

  1. Greg Willits

    Greg Willits Guest

    '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/.
     
    Greg Willits, Oct 27, 2007
    #1
    1. Advertising

  2. Greg Willits

    Greg Willits Guest

    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 :p

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

    -- gw

    --
    Posted via http://www.ruby-forum.com/.
     
    Greg Willits, Oct 27, 2007
    #2
    1. Advertising

  3. -----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-----
     
    Eustáquio 'TaQ' Rangel, Oct 27, 2007
    #3
  4. 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
     
    Nobuyoshi Nakada, Oct 27, 2007
    #4
  5. 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/.
     
    Wolfgang Nádasi-Donner, Oct 27, 2007
    #5
  6. T24gMTAvMjcvMDcsIFdvbGZnYW5nIE7DoWRhc2ktRG9ubmVyIDxlZC5vZGFub3dAd29uYWRvLmRl
    PiB3cm90ZToKPiBJZiBZb3Ugd2FudCB0byBjb21wYXJlIFN0cmluZ3MgYmFzZWQgb24gdGhlIHVz
    YWdlIGluIGEgbGFuZ3VhZ2UsIHlvdQo+IHNob3VsZCBiZXR0ZXIgdXNlIGFuIGFwcHJvcHJpYXRl
    IHNlcXVlbmNlIGRlZmluaXRpb24uCgoKV2hhdCBpcyB0aGUgYmVzdCB3YXkgdG8gY3JlYXRlIGEg
    bmV3IHNvcnQgb3JkZXI/CkZvciBleGFtcGxlLCBpZiB0aGUgb3JkZXIgaXMgdGhpczoKJXdbMSAy
    IDMgNCA1IDYgNyA4IDkgMCBBIGEgQiBiLi4uXQphbmQgZXZlcnkgb3RoZXIgY2hhcmFjdGVyIHRv
    IGJlIHNvcnRlZCB1c2luZyBBcnJheSNzb3J0IGFmdGVyIHRoZQphbHBoYW51bWVyaWMgY2hhcmFj
    dGVycy4KCi0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0KRGFuaWVs
    IEJydW1iYXVnaCBLZWVuZXkKRGV2aSBXZWIgRGV2ZWxvcG1lbnQKRGV2aS5XZWJNYXN0ZXJAZ01h
    aWwuY29tCi0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0K
     
    Devi Web Development, Oct 27, 2007
    #6
  7. On 10/27/07, Devi Web Development <> 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

    -------------------------------------------
     
    Devi Web Development, Oct 27, 2007
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alan
    Replies:
    7
    Views:
    434
  2. =?Utf-8?B?UHJhc2hhbnQgQw==?=

    Text Comparison

    =?Utf-8?B?UHJhc2hhbnQgQw==?=, Aug 8, 2007, in forum: ASP .Net
    Replies:
    0
    Views:
    274
    =?Utf-8?B?UHJhc2hhbnQgQw==?=
    Aug 8, 2007
  3. Markus Hillbrand

    Ruby speed comparison

    Markus Hillbrand, Oct 19, 2003, in forum: Ruby
    Replies:
    2
    Views:
    82
    Robert Klemme
    Oct 19, 2003
  4. Replies:
    11
    Views:
    235
    brian d foy
    Dec 16, 2006
  5. Deepu
    Replies:
    1
    Views:
    244
    ccc31807
    Feb 7, 2011
Loading...

Share This Page