Sort hash with keys as integer

Discussion in 'Ruby' started by Pål Bergström, Sep 25, 2010.

  1. I have problems with sorting a hash. I get this sort 1,10,2,3..... Must
    be because the key is a string, right? How do I deal with this? How do I
    make the key value into an integer?
    --
    Posted via http://www.ruby-forum.com/.
    Pål Bergström, Sep 25, 2010
    #1
    1. Advertising

  2. Pål Bergström

    Y. NOBUOKA Guest

    > I have problems with sorting a hash. I get this sort 1,10,2,3.....
    > Must be because the key is a string, right?


    I think that your idea is right, but I can't conclude it.
    Please show us your code.

    > How do I deal with this? How do I
    > make the key value into an integer?


    Using String#to_i method [
    http://ruby-doc.org/core/classes/String.html#M000787 ],
    you can convert a String object to a Integer object.

    For instance:

    # you want to sort this hash
    hash = { "1" => "test", "2" => "aaa", "10" => "bbb" }
    # sort : not expectation
    p hash.sort { |a, b| a[0] <=> b[0] }
    # sort : expectation
    p hash.sort { |a, b| a[0].to_i <=> b[0].to_i }

    --
    NOBUOKA Yuya
    Y. NOBUOKA, Sep 25, 2010
    #2
    1. Advertising

  3. Y. NOBUOKA wrote:

    > p hash.sort { |a, b| a[0].to_i <=> b[0].to_i }


    I tried this myself but it didn't work. Now it did. Strange. Must have
    done something wrong before. Thanks :)
    --
    Posted via http://www.ruby-forum.com/.
    Pål Bergström, Sep 25, 2010
    #3
  4. 2010/9/25 P=E5l Bergstr=F6m <>:
    > Y. NOBUOKA wrote:
    >
    >> p hash.sort { |a, b| a[0].to_i <=3D> b[0].to_i }

    >
    > I tried this myself but it didn't work. Now it did. Strange. Must have
    > done something wrong before. Thanks :)


    This also works:

    irb(main):001:0> { "1" =3D> "test", "2" =3D> "aaa", "10" =3D> "bbb"
    }.sort_by {|k,v| k.to_i}
    =3D> [["1", "test"], ["2", "aaa"], ["10", "bbb"]]
    irb(main):002:0> { "1" =3D> "test", "2" =3D> "aaa", "10" =3D> "bbb"
    }.sort_by {|k,v| Integer(k)}
    =3D> [["1", "test"], ["2", "aaa"], ["10", "bbb"]]

    Cheers

    robert

    --=20
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
    Robert Klemme, Sep 26, 2010
    #4
    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. Tommo
    Replies:
    2
    Views:
    3,469
    Gunnar Hjalmarsson
    Dec 15, 2003
  2. rp
    Replies:
    1
    Views:
    480
    red floyd
    Nov 10, 2011
  3. Malik Yousef

    Sort Hash o Hash accordint to two keys

    Malik Yousef, May 6, 2004, in forum: Perl Misc
    Replies:
    9
    Views:
    184
    Uri Guttman
    May 7, 2004
  4. Malik Yousef

    Sort Hash o Hash accordint to two keys

    Malik Yousef, May 6, 2004, in forum: Perl Misc
    Replies:
    0
    Views:
    95
    Malik Yousef
    May 6, 2004
  5. mickey

    Sort keys in a hash numerically

    mickey, Aug 8, 2006, in forum: Perl Misc
    Replies:
    5
    Views:
    210
    -berlin.de
    Aug 11, 2006
Loading...

Share This Page