How to sort an array of arrays in ascending orderCreate ArrayList from arrayHow do I check if an array includes an object in JavaScript?How to append something to an array?How to insert an item into an array at a specific index (JavaScript)?Sort array of objects by string property valueHow do I empty an array in JavaScript?Loop through an array in JavaScriptHow to check if an object is an array?How do I remove a particular element from an array in JavaScript?For-each over an array in JavaScript?
What is this "opened" cube called?
What should be done with the carbon when using magic to get oxygen from carbon dioxide?
Is "prohibition against," a double negative?
Was it illegal to blaspheme God in Antioch in 360.-410.?
Printing a list as "a, b, c." using Python
Get contents before a colon
What caused the end of cybernetic implants?
Inspiration for failed idea?
Does the telecom provider need physical access to the SIM card to clone it?
How do I portray irrational anger in first person?
What is the practical impact of using System.Random which is not cryptographically random?
German equivalent to "going down the rabbit hole"
Sum and average calculator
How do I get my neighbour to stop disturbing with loud music?
Can authors email you PDFs of their textbook for free?
How can I improve my formal definitions
Small RAM 4 KB on the early Apple II?
Don't look at what I did there
Did the Apollo Guidance Computer really use 60% of the world's ICs in 1963?
In what language did Túrin converse with Mím?
How to understand payment due date for credit card?
How to investigate an unknown 1.5GB file named "sudo" in my Linux home directory?
Unexpected behavior after assignment of function object to function wrapper
Is Borg adaptation only temporary?
How to sort an array of arrays in ascending order
Create ArrayList from arrayHow do I check if an array includes an object in JavaScript?How to append something to an array?How to insert an item into an array at a specific index (JavaScript)?Sort array of objects by string property valueHow do I empty an array in JavaScript?Loop through an array in JavaScriptHow to check if an object is an array?How do I remove a particular element from an array in JavaScript?For-each over an array in JavaScript?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
Information in an array:
scores = %w[ScoreA ScoreB ScoreC ScoreD ScoreE ScoreF ScoreG ScoreH ScoreI ScoreJ]
needs to be presented in ascending order of the golf scores.
Can anyone help sorting the output in ascending order?
golf = scores.map do |score_number|
print "Enter the score for #score_number:"
[score_number, gets.to_i]
end
puts golf.sort
arrays ruby
add a comment |
Information in an array:
scores = %w[ScoreA ScoreB ScoreC ScoreD ScoreE ScoreF ScoreG ScoreH ScoreI ScoreJ]
needs to be presented in ascending order of the golf scores.
Can anyone help sorting the output in ascending order?
golf = scores.map do |score_number|
print "Enter the score for #score_number:"
[score_number, gets.to_i]
end
puts golf.sort
arrays ruby
1
"Any tips?" is hardly a proper question. Please have a look at to ask a good question and take your time to format the code properly. You can see a preview of your post in the bottom of the editing page.
– dedObed
Mar 27 at 23:25
@dedObed, Thank you for your input. Both are edited.
– C_B
Mar 27 at 23:30
add a comment |
Information in an array:
scores = %w[ScoreA ScoreB ScoreC ScoreD ScoreE ScoreF ScoreG ScoreH ScoreI ScoreJ]
needs to be presented in ascending order of the golf scores.
Can anyone help sorting the output in ascending order?
golf = scores.map do |score_number|
print "Enter the score for #score_number:"
[score_number, gets.to_i]
end
puts golf.sort
arrays ruby
Information in an array:
scores = %w[ScoreA ScoreB ScoreC ScoreD ScoreE ScoreF ScoreG ScoreH ScoreI ScoreJ]
needs to be presented in ascending order of the golf scores.
Can anyone help sorting the output in ascending order?
golf = scores.map do |score_number|
print "Enter the score for #score_number:"
[score_number, gets.to_i]
end
puts golf.sort
arrays ruby
arrays ruby
edited Mar 28 at 9:46
sawa
136k31 gold badges221 silver badges319 bronze badges
136k31 gold badges221 silver badges319 bronze badges
asked Mar 27 at 22:54
C_BC_B
41 bronze badge
41 bronze badge
1
"Any tips?" is hardly a proper question. Please have a look at to ask a good question and take your time to format the code properly. You can see a preview of your post in the bottom of the editing page.
– dedObed
Mar 27 at 23:25
@dedObed, Thank you for your input. Both are edited.
– C_B
Mar 27 at 23:30
add a comment |
1
"Any tips?" is hardly a proper question. Please have a look at to ask a good question and take your time to format the code properly. You can see a preview of your post in the bottom of the editing page.
– dedObed
Mar 27 at 23:25
@dedObed, Thank you for your input. Both are edited.
– C_B
Mar 27 at 23:30
1
1
"Any tips?" is hardly a proper question. Please have a look at to ask a good question and take your time to format the code properly. You can see a preview of your post in the bottom of the editing page.
– dedObed
Mar 27 at 23:25
"Any tips?" is hardly a proper question. Please have a look at to ask a good question and take your time to format the code properly. You can see a preview of your post in the bottom of the editing page.
– dedObed
Mar 27 at 23:25
@dedObed, Thank you for your input. Both are edited.
– C_B
Mar 27 at 23:30
@dedObed, Thank you for your input. Both are edited.
– C_B
Mar 27 at 23:30
add a comment |
2 Answers
2
active
oldest
votes
Just use Array#sort with block
golf.sort a.last <=> b.last
or Enumerable#sort_by
golf.sort_by
The second variant can be shortened using Proc
golf.sort_by(&:last)
@ray Your edit changes the code's intention. That is too much.
– sawa
Mar 28 at 9:43
You can address block assort_by(&:last)with to format short in length.
– ray
Mar 28 at 9:48
@ray, I've updated my answer and added the link to explain it.
– mechnicov
Mar 28 at 10:04
add a comment |
Just use Array#sort with block
golf.sort x, y
=> [["ScoreH", 1], ["ScoreB", 3], ["ScoreD", 4], ["ScoreF", 9], ["ScoreA", 10], ["ScoreJ", 23], ["ScoreG", 45], ["ScoreC", 67], ["ScoreI", 87], ["ScoreE", 88]]
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55387704%2fhow-to-sort-an-array-of-arrays-in-ascending-order%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Just use Array#sort with block
golf.sort a.last <=> b.last
or Enumerable#sort_by
golf.sort_by
The second variant can be shortened using Proc
golf.sort_by(&:last)
@ray Your edit changes the code's intention. That is too much.
– sawa
Mar 28 at 9:43
You can address block assort_by(&:last)with to format short in length.
– ray
Mar 28 at 9:48
@ray, I've updated my answer and added the link to explain it.
– mechnicov
Mar 28 at 10:04
add a comment |
Just use Array#sort with block
golf.sort a.last <=> b.last
or Enumerable#sort_by
golf.sort_by
The second variant can be shortened using Proc
golf.sort_by(&:last)
@ray Your edit changes the code's intention. That is too much.
– sawa
Mar 28 at 9:43
You can address block assort_by(&:last)with to format short in length.
– ray
Mar 28 at 9:48
@ray, I've updated my answer and added the link to explain it.
– mechnicov
Mar 28 at 10:04
add a comment |
Just use Array#sort with block
golf.sort a.last <=> b.last
or Enumerable#sort_by
golf.sort_by
The second variant can be shortened using Proc
golf.sort_by(&:last)
Just use Array#sort with block
golf.sort a.last <=> b.last
or Enumerable#sort_by
golf.sort_by
The second variant can be shortened using Proc
golf.sort_by(&:last)
edited Mar 28 at 10:01
answered Mar 27 at 23:41
mechnicovmechnicov
2,4412 gold badges9 silver badges24 bronze badges
2,4412 gold badges9 silver badges24 bronze badges
@ray Your edit changes the code's intention. That is too much.
– sawa
Mar 28 at 9:43
You can address block assort_by(&:last)with to format short in length.
– ray
Mar 28 at 9:48
@ray, I've updated my answer and added the link to explain it.
– mechnicov
Mar 28 at 10:04
add a comment |
@ray Your edit changes the code's intention. That is too much.
– sawa
Mar 28 at 9:43
You can address block assort_by(&:last)with to format short in length.
– ray
Mar 28 at 9:48
@ray, I've updated my answer and added the link to explain it.
– mechnicov
Mar 28 at 10:04
@ray Your edit changes the code's intention. That is too much.
– sawa
Mar 28 at 9:43
@ray Your edit changes the code's intention. That is too much.
– sawa
Mar 28 at 9:43
You can address block as
sort_by(&:last) with to format short in length.– ray
Mar 28 at 9:48
You can address block as
sort_by(&:last) with to format short in length.– ray
Mar 28 at 9:48
@ray, I've updated my answer and added the link to explain it.
– mechnicov
Mar 28 at 10:04
@ray, I've updated my answer and added the link to explain it.
– mechnicov
Mar 28 at 10:04
add a comment |
Just use Array#sort with block
golf.sort x, y
=> [["ScoreH", 1], ["ScoreB", 3], ["ScoreD", 4], ["ScoreF", 9], ["ScoreA", 10], ["ScoreJ", 23], ["ScoreG", 45], ["ScoreC", 67], ["ScoreI", 87], ["ScoreE", 88]]
add a comment |
Just use Array#sort with block
golf.sort x, y
=> [["ScoreH", 1], ["ScoreB", 3], ["ScoreD", 4], ["ScoreF", 9], ["ScoreA", 10], ["ScoreJ", 23], ["ScoreG", 45], ["ScoreC", 67], ["ScoreI", 87], ["ScoreE", 88]]
add a comment |
Just use Array#sort with block
golf.sort x, y
=> [["ScoreH", 1], ["ScoreB", 3], ["ScoreD", 4], ["ScoreF", 9], ["ScoreA", 10], ["ScoreJ", 23], ["ScoreG", 45], ["ScoreC", 67], ["ScoreI", 87], ["ScoreE", 88]]
Just use Array#sort with block
golf.sort x, y
=> [["ScoreH", 1], ["ScoreB", 3], ["ScoreD", 4], ["ScoreF", 9], ["ScoreA", 10], ["ScoreJ", 23], ["ScoreG", 45], ["ScoreC", 67], ["ScoreI", 87], ["ScoreE", 88]]
edited Mar 28 at 7:18
Ajay Barot
1,4371 gold badge19 silver badges33 bronze badges
1,4371 gold badge19 silver badges33 bronze badges
answered Mar 28 at 4:50
Srinidhi GsSrinidhi Gs
604 bronze badges
604 bronze badges
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55387704%2fhow-to-sort-an-array-of-arrays-in-ascending-order%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
"Any tips?" is hardly a proper question. Please have a look at to ask a good question and take your time to format the code properly. You can see a preview of your post in the bottom of the editing page.
– dedObed
Mar 27 at 23:25
@dedObed, Thank you for your input. Both are edited.
– C_B
Mar 27 at 23:30