Skip to content

Commit

Permalink
Literal Array #sort_by! (#272)
Browse files Browse the repository at this point in the history
Closes #205.

Plus changes the title of the `#sort` test and adds one for `#sort!`.
  • Loading branch information
hslzr authored Jan 6, 2025
1 parent 20a1892 commit e93c3ec
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
5 changes: 5 additions & 0 deletions lib/literal/array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,11 @@ def sort!(...)
self
end

def sort_by!(...)
@__value__.sort_by!(...)
self
end

def take(...)
__with__(@__value__.take(...))
end
Expand Down
16 changes: 15 additions & 1 deletion test/array.test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@
assert_equal (array <=> other), -1
end

test "#sort sorts the array" do
test "#sort returns a new sorted array" do
array = Literal::Array(Integer).new(3, 2, 1)

result = array.sort
Expand All @@ -259,6 +259,20 @@
assert_equal result.to_a, [1, 2, 3]
end

test "#sort! sorts the array in place" do
array = Literal::Array(Integer).new(3, 2, 1)
array.sort!

assert_equal array.to_a, [1, 2, 3]
end

test "#sort_by! sorts the array given a block" do
array = Literal::Array(String).new("Lucy", "Stephen", "Bob")
array.sort_by!(&:size)

assert_equal array.to_a, ["Bob", "Lucy", "Stephen"]
end

test "#push appends single value" do
array = Literal::Array(Integer).new(1, 2, 3)

Expand Down

0 comments on commit e93c3ec

Please sign in to comment.