Which gem has a class like 'sorted list'?

For example:

sl = SortedList.new {|x| -x }

the block is sth just like for sort_by method

sl << 2 << 6 << 10 << 3 << 8

each time inserting an elem into the list,

the elem will be put at the proper position

sl # will be [10, 8, 6, 3, 2]

Is there any gem has such class? Maybe there’s no need to invent the
wheels?

Joey Z. wrote in post #1053482:

For example:

sl = SortedList.new {|x| -x }

the block is sth just like for sort_by method

sl << 2 << 6 << 10 << 3 << 8

each time inserting an elem into the list,

the elem will be put at the proper position

sl # will be [10, 8, 6, 3, 2]

Is there any gem has such class? Maybe there’s no need to invent the
wheels?

There are libs for binary search

http://raa.ruby-lang.org/search.rhtml?search=binary%20search

and “sorted list” also has some hits

http://raa.ruby-lang.org/search.rhtml?search=sorted%20list

What do you need that for?

Kind regards

robert

hi Joey,

maybe i misunderstand the question, but is there a reason that
Array#sort doesn’t work for you?

array = [2, 6, 10, 3, 8]

sorted = array.sort.reverse

p sorted

=> [10, 8, 6, 3, 2]

you could easily write a method for the Array class that does this
every time you add an element to the array…

  • j

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs