Custom comparison for integer strings












1















For the input 3 30 34 5 9, the expected largest number output is: 9534330



Any inputs why the sorted is returning the same result as input: 3303459?



import functools

def compare(item1, item2):
return int(item1 + item2) - int(item2 + item1)

inputList = input().split()
sorted(inputList, key=functools.cmp_to_key(compare))
max = "".join([ x for x in inputList])
print(max)









share|improve this question




















  • 4





    Because sorted returns a new list.

    – jonrsharpe
    Nov 20 '18 at 7:56


















1















For the input 3 30 34 5 9, the expected largest number output is: 9534330



Any inputs why the sorted is returning the same result as input: 3303459?



import functools

def compare(item1, item2):
return int(item1 + item2) - int(item2 + item1)

inputList = input().split()
sorted(inputList, key=functools.cmp_to_key(compare))
max = "".join([ x for x in inputList])
print(max)









share|improve this question




















  • 4





    Because sorted returns a new list.

    – jonrsharpe
    Nov 20 '18 at 7:56
















1












1








1








For the input 3 30 34 5 9, the expected largest number output is: 9534330



Any inputs why the sorted is returning the same result as input: 3303459?



import functools

def compare(item1, item2):
return int(item1 + item2) - int(item2 + item1)

inputList = input().split()
sorted(inputList, key=functools.cmp_to_key(compare))
max = "".join([ x for x in inputList])
print(max)









share|improve this question
















For the input 3 30 34 5 9, the expected largest number output is: 9534330



Any inputs why the sorted is returning the same result as input: 3303459?



import functools

def compare(item1, item2):
return int(item1 + item2) - int(item2 + item1)

inputList = input().split()
sorted(inputList, key=functools.cmp_to_key(compare))
max = "".join([ x for x in inputList])
print(max)






python python-3.x






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 8:00









jonrsharpe

77.6k11104211




77.6k11104211










asked Nov 20 '18 at 7:54









RockRock

8117




8117








  • 4





    Because sorted returns a new list.

    – jonrsharpe
    Nov 20 '18 at 7:56
















  • 4





    Because sorted returns a new list.

    – jonrsharpe
    Nov 20 '18 at 7:56










4




4





Because sorted returns a new list.

– jonrsharpe
Nov 20 '18 at 7:56







Because sorted returns a new list.

– jonrsharpe
Nov 20 '18 at 7:56














3 Answers
3






active

oldest

votes


















1














The reason is either because you are sorting the list in the wrong order and you did not use the sorted list. Currently, you are sorting the list in an ascending order by the first digit. In addition to that, sorted() function returns a new list, so you need to save that in a variable.



You can fix that by either using the reverse() on the sorted list



import functools

def compare(item1, item2):
return int(item1 + item2) - int(item2 + item1)

inputList = input().split()
inputList = sorted(inputList, key=functools.cmp_to_key(compare))
inputList.reverse()
max = "".join([ x for x in inputList])
print(max)


Or switch operation in the compare() function



import functools

def compare(item1, item2):
return int(item2 + item1) - int(item1 + item2)

inputList = input().split()
inputList = sorted(inputList, key=functools.cmp_to_key(compare))
max = "".join([ x for x in inputList])
print(max)





share|improve this answer































    1














    For exhaustiveness, you can also set the order argument in the sorted() function:
    import functools



    def compare(item1, item2):
    return int(item1 + item2) - int(item2 + item1)

    input_list = input().split(",")
    custom_sorted_list = sorted(input_list, key=functools.cmp_to_key(compare), reverse=True)
    max = "".join([ x for x in custom_sorted_list])
    print(max)





    share|improve this answer































      0














      The sorted function returns the list in ascending order.



      But, as you only want the largest number, it is even easier:



      import itertools

      input_list = [3, 30, 34, 5, 9]

      answer = max(map("".join, itertools.permutations(map(str, input_list))))

      print(answer)





      share|improve this answer























        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
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53388492%2fcustom-comparison-for-integer-strings%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        1














        The reason is either because you are sorting the list in the wrong order and you did not use the sorted list. Currently, you are sorting the list in an ascending order by the first digit. In addition to that, sorted() function returns a new list, so you need to save that in a variable.



        You can fix that by either using the reverse() on the sorted list



        import functools

        def compare(item1, item2):
        return int(item1 + item2) - int(item2 + item1)

        inputList = input().split()
        inputList = sorted(inputList, key=functools.cmp_to_key(compare))
        inputList.reverse()
        max = "".join([ x for x in inputList])
        print(max)


        Or switch operation in the compare() function



        import functools

        def compare(item1, item2):
        return int(item2 + item1) - int(item1 + item2)

        inputList = input().split()
        inputList = sorted(inputList, key=functools.cmp_to_key(compare))
        max = "".join([ x for x in inputList])
        print(max)





        share|improve this answer




























          1














          The reason is either because you are sorting the list in the wrong order and you did not use the sorted list. Currently, you are sorting the list in an ascending order by the first digit. In addition to that, sorted() function returns a new list, so you need to save that in a variable.



          You can fix that by either using the reverse() on the sorted list



          import functools

          def compare(item1, item2):
          return int(item1 + item2) - int(item2 + item1)

          inputList = input().split()
          inputList = sorted(inputList, key=functools.cmp_to_key(compare))
          inputList.reverse()
          max = "".join([ x for x in inputList])
          print(max)


          Or switch operation in the compare() function



          import functools

          def compare(item1, item2):
          return int(item2 + item1) - int(item1 + item2)

          inputList = input().split()
          inputList = sorted(inputList, key=functools.cmp_to_key(compare))
          max = "".join([ x for x in inputList])
          print(max)





          share|improve this answer


























            1












            1








            1







            The reason is either because you are sorting the list in the wrong order and you did not use the sorted list. Currently, you are sorting the list in an ascending order by the first digit. In addition to that, sorted() function returns a new list, so you need to save that in a variable.



            You can fix that by either using the reverse() on the sorted list



            import functools

            def compare(item1, item2):
            return int(item1 + item2) - int(item2 + item1)

            inputList = input().split()
            inputList = sorted(inputList, key=functools.cmp_to_key(compare))
            inputList.reverse()
            max = "".join([ x for x in inputList])
            print(max)


            Or switch operation in the compare() function



            import functools

            def compare(item1, item2):
            return int(item2 + item1) - int(item1 + item2)

            inputList = input().split()
            inputList = sorted(inputList, key=functools.cmp_to_key(compare))
            max = "".join([ x for x in inputList])
            print(max)





            share|improve this answer













            The reason is either because you are sorting the list in the wrong order and you did not use the sorted list. Currently, you are sorting the list in an ascending order by the first digit. In addition to that, sorted() function returns a new list, so you need to save that in a variable.



            You can fix that by either using the reverse() on the sorted list



            import functools

            def compare(item1, item2):
            return int(item1 + item2) - int(item2 + item1)

            inputList = input().split()
            inputList = sorted(inputList, key=functools.cmp_to_key(compare))
            inputList.reverse()
            max = "".join([ x for x in inputList])
            print(max)


            Or switch operation in the compare() function



            import functools

            def compare(item1, item2):
            return int(item2 + item1) - int(item1 + item2)

            inputList = input().split()
            inputList = sorted(inputList, key=functools.cmp_to_key(compare))
            max = "".join([ x for x in inputList])
            print(max)






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 20 '18 at 8:07









            AndreasAndreas

            1,91131018




            1,91131018

























                1














                For exhaustiveness, you can also set the order argument in the sorted() function:
                import functools



                def compare(item1, item2):
                return int(item1 + item2) - int(item2 + item1)

                input_list = input().split(",")
                custom_sorted_list = sorted(input_list, key=functools.cmp_to_key(compare), reverse=True)
                max = "".join([ x for x in custom_sorted_list])
                print(max)





                share|improve this answer




























                  1














                  For exhaustiveness, you can also set the order argument in the sorted() function:
                  import functools



                  def compare(item1, item2):
                  return int(item1 + item2) - int(item2 + item1)

                  input_list = input().split(",")
                  custom_sorted_list = sorted(input_list, key=functools.cmp_to_key(compare), reverse=True)
                  max = "".join([ x for x in custom_sorted_list])
                  print(max)





                  share|improve this answer


























                    1












                    1








                    1







                    For exhaustiveness, you can also set the order argument in the sorted() function:
                    import functools



                    def compare(item1, item2):
                    return int(item1 + item2) - int(item2 + item1)

                    input_list = input().split(",")
                    custom_sorted_list = sorted(input_list, key=functools.cmp_to_key(compare), reverse=True)
                    max = "".join([ x for x in custom_sorted_list])
                    print(max)





                    share|improve this answer













                    For exhaustiveness, you can also set the order argument in the sorted() function:
                    import functools



                    def compare(item1, item2):
                    return int(item1 + item2) - int(item2 + item1)

                    input_list = input().split(",")
                    custom_sorted_list = sorted(input_list, key=functools.cmp_to_key(compare), reverse=True)
                    max = "".join([ x for x in custom_sorted_list])
                    print(max)






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 20 '18 at 8:11









                    leoburgyleoburgy

                    1107




                    1107























                        0














                        The sorted function returns the list in ascending order.



                        But, as you only want the largest number, it is even easier:



                        import itertools

                        input_list = [3, 30, 34, 5, 9]

                        answer = max(map("".join, itertools.permutations(map(str, input_list))))

                        print(answer)





                        share|improve this answer




























                          0














                          The sorted function returns the list in ascending order.



                          But, as you only want the largest number, it is even easier:



                          import itertools

                          input_list = [3, 30, 34, 5, 9]

                          answer = max(map("".join, itertools.permutations(map(str, input_list))))

                          print(answer)





                          share|improve this answer


























                            0












                            0








                            0







                            The sorted function returns the list in ascending order.



                            But, as you only want the largest number, it is even easier:



                            import itertools

                            input_list = [3, 30, 34, 5, 9]

                            answer = max(map("".join, itertools.permutations(map(str, input_list))))

                            print(answer)





                            share|improve this answer













                            The sorted function returns the list in ascending order.



                            But, as you only want the largest number, it is even easier:



                            import itertools

                            input_list = [3, 30, 34, 5, 9]

                            answer = max(map("".join, itertools.permutations(map(str, input_list))))

                            print(answer)






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 20 '18 at 8:12









                            Vitor SRGVitor SRG

                            41815




                            41815






























                                draft saved

                                draft discarded




















































                                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.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53388492%2fcustom-comparison-for-integer-strings%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

                                How to change which sound is reproduced for terminal bell?

                                Can I use Tabulator js library in my java Spring + Thymeleaf project?

                                Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents