Sorting an assortment of strings and integers together, while keeping them separateInputting and sorting three integersCode reuse while keeping meaning clear and avoiding unforseen consequencesAsc and desc array sort methodsSorting millions of integersSort array of objects with hierarchy by hierarchy and nameCounting numbers whose digits all go up or downFinding median of 3 elements in array, and sorting themHackerrank Insertion Sort Algorithm 1 (creating duplicates to show shifting)Sorting integers in descending orderSorting a long string with a composite of strings and integers + symbols

Why isn't KTEX's runway designation 10/28 instead of 9/27?

How to interpret the phrase "t’en a fait voir à toi"?

Installing PowerShell on 32-bit Kali OS fails

Blender - show edges angles “direction”

node command while defining a coordinate in TikZ

Simple image editor tool to draw a simple box/rectangle in an existing image

Why are on-board computers allowed to change controls without notifying the pilots?

What would you call a finite collection of unordered objects that are not necessarily distinct?

Lifted its hind leg on or lifted its hind leg towards?

How will losing mobility of one hand affect my career as a programmer?

Why is delta-v is the most useful quantity for planning space travel?

Is there any significance to the Valyrian Stone vault door of Qarth?

Who must act to prevent Brexit on March 29th?

A social experiment. What is the worst that can happen?

I2C signal and power over long range (10meter cable)

When is separating the total wavefunction into a space part and a spin part possible?

Greatest common substring

Is the next prime number always the next number divisible by the current prime number, except for any numbers previously divisible by primes?

What does the "3am" section means in manpages?

What is the term when two people sing in harmony, but they aren't singing the same notes?

Are Warlocks Arcane or Divine?

Invariance of results when scaling explanatory variables in logistic regression, is there a proof?

Is there a problem with hiding "forgot password" until it's needed?

I'm in charge of equipment buying but no one's ever happy with what I choose. How to fix this?



Sorting an assortment of strings and integers together, while keeping them separate


Inputting and sorting three integersCode reuse while keeping meaning clear and avoiding unforseen consequencesAsc and desc array sort methodsSorting millions of integersSort array of objects with hierarchy by hierarchy and nameCounting numbers whose digits all go up or downFinding median of 3 elements in array, and sorting themHackerrank Insertion Sort Algorithm 1 (creating duplicates to show shifting)Sorting integers in descending orderSorting a long string with a composite of strings and integers + symbols













2












$begingroup$


I put a solution to this coding problem together. The problem is this:




Create a function that takes an array, finds the most often repeated element(s) within it and returns it/them in an array. The function should work for both integers and strings mixed together within the input list (e.g. [1, 1, "a"]).



If there is a tie for highest occurrence, return both.



Separate integers and strings in the result.



If returning multiple elements, sort result alphabetically with numbers coming before strings.




This is the solution I came up with:



def highest_occurrence(arr)

# Separate the unique values into individual sub-arrays
x = rand(2**32).to_s(16)
result = arr.sort do |a, b|
a = a.to_s + x if a.is_a?(Numeric)
b = b.to_s + x if b.is_a?(Numeric)
a <=> b
end.chunk_while a == b .to_a

# Get an array of all of the individual values with the max size,
# Sort them by integers first, strings second
result = result.select do |a2|
a2.size == result.max_by(&:size).size
end.map(&:uniq).flatten.sort_by v

end


It passes these tests:



p highest_occurrence(["a","a","b","b"]) == ["a","b"]
p highest_occurrence([1,"a","b","b"]) == ["b"]
p highest_occurrence([1,2,2,3,3,3,4,4,4,4]) == [4]
p highest_occurrence(["ab","ab","b"]) == ["ab"]
p highest_occurrence(["ab","ab","b","bb","b"]) == ["ab","b"]
p highest_occurrence([3,3,3,4,4,4,4,2,3,6,7,6,7,6,7,6,"a","a","a","a"]) == [3,4,6,"a"]
p highest_occurrence([2,2,"2","2",4,4]) == [2,4,"2"]


I'd like to know whether there are better ways to solve some of the specific problems in this exercise. In particular, the requirement to sort strings and integers together without being able to convert the integers to strings in the sort block was an interesting one. I managed this by appending a random hex value (the same value) to each integer during the sort process. This seems a bit hackish, and I have the feeling it could be improved upon.



I would also appreciate any other suggestions for how to do a cleaner job.










share|improve this question











$endgroup$
















    2












    $begingroup$


    I put a solution to this coding problem together. The problem is this:




    Create a function that takes an array, finds the most often repeated element(s) within it and returns it/them in an array. The function should work for both integers and strings mixed together within the input list (e.g. [1, 1, "a"]).



    If there is a tie for highest occurrence, return both.



    Separate integers and strings in the result.



    If returning multiple elements, sort result alphabetically with numbers coming before strings.




    This is the solution I came up with:



    def highest_occurrence(arr)

    # Separate the unique values into individual sub-arrays
    x = rand(2**32).to_s(16)
    result = arr.sort do |a, b|
    a = a.to_s + x if a.is_a?(Numeric)
    b = b.to_s + x if b.is_a?(Numeric)
    a <=> b
    end.chunk_while a == b .to_a

    # Get an array of all of the individual values with the max size,
    # Sort them by integers first, strings second
    result = result.select do |a2|
    a2.size == result.max_by(&:size).size
    end.map(&:uniq).flatten.sort_by v

    end


    It passes these tests:



    p highest_occurrence(["a","a","b","b"]) == ["a","b"]
    p highest_occurrence([1,"a","b","b"]) == ["b"]
    p highest_occurrence([1,2,2,3,3,3,4,4,4,4]) == [4]
    p highest_occurrence(["ab","ab","b"]) == ["ab"]
    p highest_occurrence(["ab","ab","b","bb","b"]) == ["ab","b"]
    p highest_occurrence([3,3,3,4,4,4,4,2,3,6,7,6,7,6,7,6,"a","a","a","a"]) == [3,4,6,"a"]
    p highest_occurrence([2,2,"2","2",4,4]) == [2,4,"2"]


    I'd like to know whether there are better ways to solve some of the specific problems in this exercise. In particular, the requirement to sort strings and integers together without being able to convert the integers to strings in the sort block was an interesting one. I managed this by appending a random hex value (the same value) to each integer during the sort process. This seems a bit hackish, and I have the feeling it could be improved upon.



    I would also appreciate any other suggestions for how to do a cleaner job.










    share|improve this question











    $endgroup$














      2












      2








      2





      $begingroup$


      I put a solution to this coding problem together. The problem is this:




      Create a function that takes an array, finds the most often repeated element(s) within it and returns it/them in an array. The function should work for both integers and strings mixed together within the input list (e.g. [1, 1, "a"]).



      If there is a tie for highest occurrence, return both.



      Separate integers and strings in the result.



      If returning multiple elements, sort result alphabetically with numbers coming before strings.




      This is the solution I came up with:



      def highest_occurrence(arr)

      # Separate the unique values into individual sub-arrays
      x = rand(2**32).to_s(16)
      result = arr.sort do |a, b|
      a = a.to_s + x if a.is_a?(Numeric)
      b = b.to_s + x if b.is_a?(Numeric)
      a <=> b
      end.chunk_while a == b .to_a

      # Get an array of all of the individual values with the max size,
      # Sort them by integers first, strings second
      result = result.select do |a2|
      a2.size == result.max_by(&:size).size
      end.map(&:uniq).flatten.sort_by v

      end


      It passes these tests:



      p highest_occurrence(["a","a","b","b"]) == ["a","b"]
      p highest_occurrence([1,"a","b","b"]) == ["b"]
      p highest_occurrence([1,2,2,3,3,3,4,4,4,4]) == [4]
      p highest_occurrence(["ab","ab","b"]) == ["ab"]
      p highest_occurrence(["ab","ab","b","bb","b"]) == ["ab","b"]
      p highest_occurrence([3,3,3,4,4,4,4,2,3,6,7,6,7,6,7,6,"a","a","a","a"]) == [3,4,6,"a"]
      p highest_occurrence([2,2,"2","2",4,4]) == [2,4,"2"]


      I'd like to know whether there are better ways to solve some of the specific problems in this exercise. In particular, the requirement to sort strings and integers together without being able to convert the integers to strings in the sort block was an interesting one. I managed this by appending a random hex value (the same value) to each integer during the sort process. This seems a bit hackish, and I have the feeling it could be improved upon.



      I would also appreciate any other suggestions for how to do a cleaner job.










      share|improve this question











      $endgroup$




      I put a solution to this coding problem together. The problem is this:




      Create a function that takes an array, finds the most often repeated element(s) within it and returns it/them in an array. The function should work for both integers and strings mixed together within the input list (e.g. [1, 1, "a"]).



      If there is a tie for highest occurrence, return both.



      Separate integers and strings in the result.



      If returning multiple elements, sort result alphabetically with numbers coming before strings.




      This is the solution I came up with:



      def highest_occurrence(arr)

      # Separate the unique values into individual sub-arrays
      x = rand(2**32).to_s(16)
      result = arr.sort do |a, b|
      a = a.to_s + x if a.is_a?(Numeric)
      b = b.to_s + x if b.is_a?(Numeric)
      a <=> b
      end.chunk_while a == b .to_a

      # Get an array of all of the individual values with the max size,
      # Sort them by integers first, strings second
      result = result.select do |a2|
      a2.size == result.max_by(&:size).size
      end.map(&:uniq).flatten.sort_by v

      end


      It passes these tests:



      p highest_occurrence(["a","a","b","b"]) == ["a","b"]
      p highest_occurrence([1,"a","b","b"]) == ["b"]
      p highest_occurrence([1,2,2,3,3,3,4,4,4,4]) == [4]
      p highest_occurrence(["ab","ab","b"]) == ["ab"]
      p highest_occurrence(["ab","ab","b","bb","b"]) == ["ab","b"]
      p highest_occurrence([3,3,3,4,4,4,4,2,3,6,7,6,7,6,7,6,"a","a","a","a"]) == [3,4,6,"a"]
      p highest_occurrence([2,2,"2","2",4,4]) == [2,4,"2"]


      I'd like to know whether there are better ways to solve some of the specific problems in this exercise. In particular, the requirement to sort strings and integers together without being able to convert the integers to strings in the sort block was an interesting one. I managed this by appending a random hex value (the same value) to each integer during the sort process. This seems a bit hackish, and I have the feeling it could be improved upon.



      I would also appreciate any other suggestions for how to do a cleaner job.







      ruby sorting






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 13 at 2:20









      Jamal

      30.4k11121227




      30.4k11121227










      asked Mar 12 at 19:50









      BobRodesBobRodes

      1335




      1335




















          1 Answer
          1






          active

          oldest

          votes


















          3












          $begingroup$

          Your test cases should include lexical sort of integers; that is, [9,11] must return [11,9]. (Your implementation does pass this test since you're converting everything to a string).



          As you suspected, mangling the input is hacky. This is better accomplished with a multi-criteria sort. This technique maps each individual value to an array of sort criteria. Ruby will compare the arrays only until it finds unequal elements; this means you can mix integers and strings in the second field, so long as the first field distinguishes them.



          For our problem, the first field will be 0 for integers else 1. The second field is the value itself.



          Although this approach will never compare integers to strings—if the first criteria distinguishes the two elements, the comparison is done—the second criteria must be a string anyway, to satisfy the "sort alphabetically" requirement.



          It's not necessary to sort the entire array or store a full copy of it. Instead, count duplicates in a hash table. Traverse the values of the hash to find a maximum. Traverse again to extract the corresponding keys.



          def highest_occurrence(arr)
          return arr if arr.size <= 1
          count = Hash.new(0)
          arr.each count[k] += 1
          max = count.max_byk,n[1]
          return count.select k,n.keys.sort_by k
          end





          share|improve this answer











          $endgroup$












          • $begingroup$
            Thanks for your answer and your very helpful example. I've stepped through it, and it's entirely clear. Very nice upgrade to mine! Two things I don't quite understand: the spec says to return integers sorted in ascending order, followed by strings sorted in ascending order. Why, then, should [9,11] return 11,9]? Also, why can't you just do .sort_by ? What test cases would fail to that? None of miine do.
            $endgroup$
            – BobRodes
            Mar 13 at 1:26







          • 1




            $begingroup$
            I answered my second question, once I went to the trouble of reading the link that you posted and doing a few other test cases. All my test cases happen to have the max values in the proper sorted order already! I switched them around and they failed. So, that's how you do a nested sort: put the criteria in an array. I'll put that in the file. Also, now that I look at it, I think that's what you were trying to tell me up front. Is it possible that you intended to say that [11, 9] should return '[9,11]`?
            $endgroup$
            – BobRodes
            Mar 13 at 1:43











          • $begingroup$
            One more thing. I don't think that return [] unless arr.length will ever return a [], since 0 evaluates to true in Ruby. Is this a carry-over pattern from some other language such as JS? I would put return [] if arr.size.zero? myself.
            $endgroup$
            – BobRodes
            Mar 13 at 1:50







          • 1




            $begingroup$
            You post said "sort result alphabetically" which to me means "lexically," implying 9 vs 11 should sort the same way "9" vs "11" does: with 11 first. It's an odd phrasing and maybe just sloppiness on the part of the problem's author. If this were a contract I'd ask the client if that's what they really wanted. To sort the integers numerically, simply remove .to_s at the very end.
            $endgroup$
            – Oh My Goodness
            Mar 13 at 2:46







          • 1




            $begingroup$
            Aha, good point. Yes, so would I! I'll mention this in the comments section to the problem. Thanks again for your help; this has been very instructive.
            $endgroup$
            – BobRodes
            Mar 13 at 7:09











          Your Answer





          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          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: "196"
          ;
          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: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          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
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f215294%2fsorting-an-assortment-of-strings-and-integers-together-while-keeping-them-separ%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          3












          $begingroup$

          Your test cases should include lexical sort of integers; that is, [9,11] must return [11,9]. (Your implementation does pass this test since you're converting everything to a string).



          As you suspected, mangling the input is hacky. This is better accomplished with a multi-criteria sort. This technique maps each individual value to an array of sort criteria. Ruby will compare the arrays only until it finds unequal elements; this means you can mix integers and strings in the second field, so long as the first field distinguishes them.



          For our problem, the first field will be 0 for integers else 1. The second field is the value itself.



          Although this approach will never compare integers to strings—if the first criteria distinguishes the two elements, the comparison is done—the second criteria must be a string anyway, to satisfy the "sort alphabetically" requirement.



          It's not necessary to sort the entire array or store a full copy of it. Instead, count duplicates in a hash table. Traverse the values of the hash to find a maximum. Traverse again to extract the corresponding keys.



          def highest_occurrence(arr)
          return arr if arr.size <= 1
          count = Hash.new(0)
          arr.each count[k] += 1
          max = count.max_byk,n[1]
          return count.select k,n.keys.sort_by k
          end





          share|improve this answer











          $endgroup$












          • $begingroup$
            Thanks for your answer and your very helpful example. I've stepped through it, and it's entirely clear. Very nice upgrade to mine! Two things I don't quite understand: the spec says to return integers sorted in ascending order, followed by strings sorted in ascending order. Why, then, should [9,11] return 11,9]? Also, why can't you just do .sort_by ? What test cases would fail to that? None of miine do.
            $endgroup$
            – BobRodes
            Mar 13 at 1:26







          • 1




            $begingroup$
            I answered my second question, once I went to the trouble of reading the link that you posted and doing a few other test cases. All my test cases happen to have the max values in the proper sorted order already! I switched them around and they failed. So, that's how you do a nested sort: put the criteria in an array. I'll put that in the file. Also, now that I look at it, I think that's what you were trying to tell me up front. Is it possible that you intended to say that [11, 9] should return '[9,11]`?
            $endgroup$
            – BobRodes
            Mar 13 at 1:43











          • $begingroup$
            One more thing. I don't think that return [] unless arr.length will ever return a [], since 0 evaluates to true in Ruby. Is this a carry-over pattern from some other language such as JS? I would put return [] if arr.size.zero? myself.
            $endgroup$
            – BobRodes
            Mar 13 at 1:50







          • 1




            $begingroup$
            You post said "sort result alphabetically" which to me means "lexically," implying 9 vs 11 should sort the same way "9" vs "11" does: with 11 first. It's an odd phrasing and maybe just sloppiness on the part of the problem's author. If this were a contract I'd ask the client if that's what they really wanted. To sort the integers numerically, simply remove .to_s at the very end.
            $endgroup$
            – Oh My Goodness
            Mar 13 at 2:46







          • 1




            $begingroup$
            Aha, good point. Yes, so would I! I'll mention this in the comments section to the problem. Thanks again for your help; this has been very instructive.
            $endgroup$
            – BobRodes
            Mar 13 at 7:09
















          3












          $begingroup$

          Your test cases should include lexical sort of integers; that is, [9,11] must return [11,9]. (Your implementation does pass this test since you're converting everything to a string).



          As you suspected, mangling the input is hacky. This is better accomplished with a multi-criteria sort. This technique maps each individual value to an array of sort criteria. Ruby will compare the arrays only until it finds unequal elements; this means you can mix integers and strings in the second field, so long as the first field distinguishes them.



          For our problem, the first field will be 0 for integers else 1. The second field is the value itself.



          Although this approach will never compare integers to strings—if the first criteria distinguishes the two elements, the comparison is done—the second criteria must be a string anyway, to satisfy the "sort alphabetically" requirement.



          It's not necessary to sort the entire array or store a full copy of it. Instead, count duplicates in a hash table. Traverse the values of the hash to find a maximum. Traverse again to extract the corresponding keys.



          def highest_occurrence(arr)
          return arr if arr.size <= 1
          count = Hash.new(0)
          arr.each count[k] += 1
          max = count.max_byk,n[1]
          return count.select k,n.keys.sort_by k
          end





          share|improve this answer











          $endgroup$












          • $begingroup$
            Thanks for your answer and your very helpful example. I've stepped through it, and it's entirely clear. Very nice upgrade to mine! Two things I don't quite understand: the spec says to return integers sorted in ascending order, followed by strings sorted in ascending order. Why, then, should [9,11] return 11,9]? Also, why can't you just do .sort_by ? What test cases would fail to that? None of miine do.
            $endgroup$
            – BobRodes
            Mar 13 at 1:26







          • 1




            $begingroup$
            I answered my second question, once I went to the trouble of reading the link that you posted and doing a few other test cases. All my test cases happen to have the max values in the proper sorted order already! I switched them around and they failed. So, that's how you do a nested sort: put the criteria in an array. I'll put that in the file. Also, now that I look at it, I think that's what you were trying to tell me up front. Is it possible that you intended to say that [11, 9] should return '[9,11]`?
            $endgroup$
            – BobRodes
            Mar 13 at 1:43











          • $begingroup$
            One more thing. I don't think that return [] unless arr.length will ever return a [], since 0 evaluates to true in Ruby. Is this a carry-over pattern from some other language such as JS? I would put return [] if arr.size.zero? myself.
            $endgroup$
            – BobRodes
            Mar 13 at 1:50







          • 1




            $begingroup$
            You post said "sort result alphabetically" which to me means "lexically," implying 9 vs 11 should sort the same way "9" vs "11" does: with 11 first. It's an odd phrasing and maybe just sloppiness on the part of the problem's author. If this were a contract I'd ask the client if that's what they really wanted. To sort the integers numerically, simply remove .to_s at the very end.
            $endgroup$
            – Oh My Goodness
            Mar 13 at 2:46







          • 1




            $begingroup$
            Aha, good point. Yes, so would I! I'll mention this in the comments section to the problem. Thanks again for your help; this has been very instructive.
            $endgroup$
            – BobRodes
            Mar 13 at 7:09














          3












          3








          3





          $begingroup$

          Your test cases should include lexical sort of integers; that is, [9,11] must return [11,9]. (Your implementation does pass this test since you're converting everything to a string).



          As you suspected, mangling the input is hacky. This is better accomplished with a multi-criteria sort. This technique maps each individual value to an array of sort criteria. Ruby will compare the arrays only until it finds unequal elements; this means you can mix integers and strings in the second field, so long as the first field distinguishes them.



          For our problem, the first field will be 0 for integers else 1. The second field is the value itself.



          Although this approach will never compare integers to strings—if the first criteria distinguishes the two elements, the comparison is done—the second criteria must be a string anyway, to satisfy the "sort alphabetically" requirement.



          It's not necessary to sort the entire array or store a full copy of it. Instead, count duplicates in a hash table. Traverse the values of the hash to find a maximum. Traverse again to extract the corresponding keys.



          def highest_occurrence(arr)
          return arr if arr.size <= 1
          count = Hash.new(0)
          arr.each count[k] += 1
          max = count.max_byk,n[1]
          return count.select k,n.keys.sort_by k
          end





          share|improve this answer











          $endgroup$



          Your test cases should include lexical sort of integers; that is, [9,11] must return [11,9]. (Your implementation does pass this test since you're converting everything to a string).



          As you suspected, mangling the input is hacky. This is better accomplished with a multi-criteria sort. This technique maps each individual value to an array of sort criteria. Ruby will compare the arrays only until it finds unequal elements; this means you can mix integers and strings in the second field, so long as the first field distinguishes them.



          For our problem, the first field will be 0 for integers else 1. The second field is the value itself.



          Although this approach will never compare integers to strings—if the first criteria distinguishes the two elements, the comparison is done—the second criteria must be a string anyway, to satisfy the "sort alphabetically" requirement.



          It's not necessary to sort the entire array or store a full copy of it. Instead, count duplicates in a hash table. Traverse the values of the hash to find a maximum. Traverse again to extract the corresponding keys.



          def highest_occurrence(arr)
          return arr if arr.size <= 1
          count = Hash.new(0)
          arr.each count[k] += 1
          max = count.max_byk,n[1]
          return count.select k,n.keys.sort_by k
          end






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 12 mins ago

























          answered Mar 13 at 0:58









          Oh My GoodnessOh My Goodness

          1,809314




          1,809314











          • $begingroup$
            Thanks for your answer and your very helpful example. I've stepped through it, and it's entirely clear. Very nice upgrade to mine! Two things I don't quite understand: the spec says to return integers sorted in ascending order, followed by strings sorted in ascending order. Why, then, should [9,11] return 11,9]? Also, why can't you just do .sort_by ? What test cases would fail to that? None of miine do.
            $endgroup$
            – BobRodes
            Mar 13 at 1:26







          • 1




            $begingroup$
            I answered my second question, once I went to the trouble of reading the link that you posted and doing a few other test cases. All my test cases happen to have the max values in the proper sorted order already! I switched them around and they failed. So, that's how you do a nested sort: put the criteria in an array. I'll put that in the file. Also, now that I look at it, I think that's what you were trying to tell me up front. Is it possible that you intended to say that [11, 9] should return '[9,11]`?
            $endgroup$
            – BobRodes
            Mar 13 at 1:43











          • $begingroup$
            One more thing. I don't think that return [] unless arr.length will ever return a [], since 0 evaluates to true in Ruby. Is this a carry-over pattern from some other language such as JS? I would put return [] if arr.size.zero? myself.
            $endgroup$
            – BobRodes
            Mar 13 at 1:50







          • 1




            $begingroup$
            You post said "sort result alphabetically" which to me means "lexically," implying 9 vs 11 should sort the same way "9" vs "11" does: with 11 first. It's an odd phrasing and maybe just sloppiness on the part of the problem's author. If this were a contract I'd ask the client if that's what they really wanted. To sort the integers numerically, simply remove .to_s at the very end.
            $endgroup$
            – Oh My Goodness
            Mar 13 at 2:46







          • 1




            $begingroup$
            Aha, good point. Yes, so would I! I'll mention this in the comments section to the problem. Thanks again for your help; this has been very instructive.
            $endgroup$
            – BobRodes
            Mar 13 at 7:09

















          • $begingroup$
            Thanks for your answer and your very helpful example. I've stepped through it, and it's entirely clear. Very nice upgrade to mine! Two things I don't quite understand: the spec says to return integers sorted in ascending order, followed by strings sorted in ascending order. Why, then, should [9,11] return 11,9]? Also, why can't you just do .sort_by ? What test cases would fail to that? None of miine do.
            $endgroup$
            – BobRodes
            Mar 13 at 1:26







          • 1




            $begingroup$
            I answered my second question, once I went to the trouble of reading the link that you posted and doing a few other test cases. All my test cases happen to have the max values in the proper sorted order already! I switched them around and they failed. So, that's how you do a nested sort: put the criteria in an array. I'll put that in the file. Also, now that I look at it, I think that's what you were trying to tell me up front. Is it possible that you intended to say that [11, 9] should return '[9,11]`?
            $endgroup$
            – BobRodes
            Mar 13 at 1:43











          • $begingroup$
            One more thing. I don't think that return [] unless arr.length will ever return a [], since 0 evaluates to true in Ruby. Is this a carry-over pattern from some other language such as JS? I would put return [] if arr.size.zero? myself.
            $endgroup$
            – BobRodes
            Mar 13 at 1:50







          • 1




            $begingroup$
            You post said "sort result alphabetically" which to me means "lexically," implying 9 vs 11 should sort the same way "9" vs "11" does: with 11 first. It's an odd phrasing and maybe just sloppiness on the part of the problem's author. If this were a contract I'd ask the client if that's what they really wanted. To sort the integers numerically, simply remove .to_s at the very end.
            $endgroup$
            – Oh My Goodness
            Mar 13 at 2:46







          • 1




            $begingroup$
            Aha, good point. Yes, so would I! I'll mention this in the comments section to the problem. Thanks again for your help; this has been very instructive.
            $endgroup$
            – BobRodes
            Mar 13 at 7:09
















          $begingroup$
          Thanks for your answer and your very helpful example. I've stepped through it, and it's entirely clear. Very nice upgrade to mine! Two things I don't quite understand: the spec says to return integers sorted in ascending order, followed by strings sorted in ascending order. Why, then, should [9,11] return 11,9]? Also, why can't you just do .sort_by ? What test cases would fail to that? None of miine do.
          $endgroup$
          – BobRodes
          Mar 13 at 1:26





          $begingroup$
          Thanks for your answer and your very helpful example. I've stepped through it, and it's entirely clear. Very nice upgrade to mine! Two things I don't quite understand: the spec says to return integers sorted in ascending order, followed by strings sorted in ascending order. Why, then, should [9,11] return 11,9]? Also, why can't you just do .sort_by ? What test cases would fail to that? None of miine do.
          $endgroup$
          – BobRodes
          Mar 13 at 1:26





          1




          1




          $begingroup$
          I answered my second question, once I went to the trouble of reading the link that you posted and doing a few other test cases. All my test cases happen to have the max values in the proper sorted order already! I switched them around and they failed. So, that's how you do a nested sort: put the criteria in an array. I'll put that in the file. Also, now that I look at it, I think that's what you were trying to tell me up front. Is it possible that you intended to say that [11, 9] should return '[9,11]`?
          $endgroup$
          – BobRodes
          Mar 13 at 1:43





          $begingroup$
          I answered my second question, once I went to the trouble of reading the link that you posted and doing a few other test cases. All my test cases happen to have the max values in the proper sorted order already! I switched them around and they failed. So, that's how you do a nested sort: put the criteria in an array. I'll put that in the file. Also, now that I look at it, I think that's what you were trying to tell me up front. Is it possible that you intended to say that [11, 9] should return '[9,11]`?
          $endgroup$
          – BobRodes
          Mar 13 at 1:43













          $begingroup$
          One more thing. I don't think that return [] unless arr.length will ever return a [], since 0 evaluates to true in Ruby. Is this a carry-over pattern from some other language such as JS? I would put return [] if arr.size.zero? myself.
          $endgroup$
          – BobRodes
          Mar 13 at 1:50





          $begingroup$
          One more thing. I don't think that return [] unless arr.length will ever return a [], since 0 evaluates to true in Ruby. Is this a carry-over pattern from some other language such as JS? I would put return [] if arr.size.zero? myself.
          $endgroup$
          – BobRodes
          Mar 13 at 1:50





          1




          1




          $begingroup$
          You post said "sort result alphabetically" which to me means "lexically," implying 9 vs 11 should sort the same way "9" vs "11" does: with 11 first. It's an odd phrasing and maybe just sloppiness on the part of the problem's author. If this were a contract I'd ask the client if that's what they really wanted. To sort the integers numerically, simply remove .to_s at the very end.
          $endgroup$
          – Oh My Goodness
          Mar 13 at 2:46





          $begingroup$
          You post said "sort result alphabetically" which to me means "lexically," implying 9 vs 11 should sort the same way "9" vs "11" does: with 11 first. It's an odd phrasing and maybe just sloppiness on the part of the problem's author. If this were a contract I'd ask the client if that's what they really wanted. To sort the integers numerically, simply remove .to_s at the very end.
          $endgroup$
          – Oh My Goodness
          Mar 13 at 2:46





          1




          1




          $begingroup$
          Aha, good point. Yes, so would I! I'll mention this in the comments section to the problem. Thanks again for your help; this has been very instructive.
          $endgroup$
          – BobRodes
          Mar 13 at 7:09





          $begingroup$
          Aha, good point. Yes, so would I! I'll mention this in the comments section to the problem. Thanks again for your help; this has been very instructive.
          $endgroup$
          – BobRodes
          Mar 13 at 7:09


















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Code Review Stack Exchange!


          • 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.

          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f215294%2fsorting-an-assortment-of-strings-and-integers-together-while-keeping-them-separ%23new-answer', 'question_page');

          );

          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







          Popular posts from this blog

          名間水力發電廠 目录 沿革 設施 鄰近設施 註釋 外部連結 导航菜单23°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.7113923°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.71139計畫概要原始内容臺灣第一座BOT 模式開發的水力發電廠-名間水力電廠名間水力發電廠 水利署首件BOT案原始内容《小檔案》名間電廠 首座BOT水力發電廠原始内容名間電廠BOT - 經濟部水利署中區水資源局

          Prove that NP is closed under karp reduction?Space(n) not closed under Karp reductions - what about NTime(n)?Class P is closed under rotation?Prove or disprove that $NL$ is closed under polynomial many-one reductions$mathbfNC_2$ is closed under log-space reductionOn Karp reductionwhen can I know if a class (complexity) is closed under reduction (cook/karp)Check if class $PSPACE$ is closed under polyonomially space reductionIs NPSPACE also closed under polynomial-time reduction and under log-space reduction?Prove PSPACE is closed under complement?Prove PSPACE is closed under union?

          Is my guitar’s action too high? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)Strings too stiff on a recently purchased acoustic guitar | Cort AD880CEIs the action of my guitar really high?Μy little finger is too weak to play guitarWith guitar, how long should I give my fingers to strengthen / callous?When playing a fret the guitar sounds mutedPlaying (Barre) chords up the guitar neckI think my guitar strings are wound too tight and I can't play barre chordsF barre chord on an SG guitarHow to find to the right strings of a barre chord by feel?High action on higher fret on my steel acoustic guitar