How to sort a string list having both number and alphabets according to the highest number [duplicate]












-1
















This question already has an answer here:




  • How to correctly sort a string with a number inside? [duplicate]

    1 answer




I am looking to sort a string list numerically which has both string and number also as a string.



['har 1', 'zee 3']



expected output :
zee 3
har 1










share|improve this question













marked as duplicate by DeepSpace, jpp python-3.x
Users with the  python-3.x badge can single-handedly close python-3.x questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 2 at 16:56


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.























    -1
















    This question already has an answer here:




    • How to correctly sort a string with a number inside? [duplicate]

      1 answer




    I am looking to sort a string list numerically which has both string and number also as a string.



    ['har 1', 'zee 3']



    expected output :
    zee 3
    har 1










    share|improve this question













    marked as duplicate by DeepSpace, jpp python-3.x
    Users with the  python-3.x badge can single-handedly close python-3.x questions as duplicates and reopen them as needed.

    StackExchange.ready(function() {
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function() {
    $hover.showInfoMessage('', {
    messageElement: $msg.clone().show(),
    transient: false,
    position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
    dismissable: false,
    relativeToBody: true
    });
    },
    function() {
    StackExchange.helpers.removeMessages();
    }
    );
    });
    });
    Jan 2 at 16:56


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.





















      -1












      -1








      -1









      This question already has an answer here:




      • How to correctly sort a string with a number inside? [duplicate]

        1 answer




      I am looking to sort a string list numerically which has both string and number also as a string.



      ['har 1', 'zee 3']



      expected output :
      zee 3
      har 1










      share|improve this question















      This question already has an answer here:




      • How to correctly sort a string with a number inside? [duplicate]

        1 answer




      I am looking to sort a string list numerically which has both string and number also as a string.



      ['har 1', 'zee 3']



      expected output :
      zee 3
      har 1





      This question already has an answer here:




      • How to correctly sort a string with a number inside? [duplicate]

        1 answer








      python-3.x






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 2 at 14:46









      Zeeshan Haris Zeeshan Haris

      62




      62




      marked as duplicate by DeepSpace, jpp python-3.x
      Users with the  python-3.x badge can single-handedly close python-3.x questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Jan 2 at 16:56


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









      marked as duplicate by DeepSpace, jpp python-3.x
      Users with the  python-3.x badge can single-handedly close python-3.x questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Jan 2 at 16:56


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


























          3 Answers
          3






          active

          oldest

          votes


















          0














          You can use key-value (dictionary) for this. Then simply sort your list according to key/value using methods given in https://www.saltycrane.com/blog/2007/09/how-to-sort-python-dictionary-by-keys/






          share|improve this answer































            0














            You can sort your list with the sorted and the argument key



            l = ['har 1', 'zee 3']
            print(sorted(l, key=lambda x: (-int(x.split()[1]), x.split()[0])))


            Output



            ['zee 3', 'har 1']


            You can even print it as you wanted with the unpack operator



            print(*sorted(l, key=lambda x: (-int(x.split()[1]), x.split()[0])), sep='n')


            Here I am creating from each value a tuple of (-int(second_element), first_element)



            So it will sort decreasingly by the numbers, and in case of equality, it will sort alphabetically






            share|improve this answer































              0














              You can use the key parameter of the sorted function to specify a custom sorting order -



              l = ['X 3', 'Z 1', 'A 2']
              l_sorted = sorted(l, key=lambda x: x.split()[-1])
              print(l_sorted)
              /*Returns ['Z 1', 'A 2', 'X 3']*/





              share|improve this answer






























                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                0














                You can use key-value (dictionary) for this. Then simply sort your list according to key/value using methods given in https://www.saltycrane.com/blog/2007/09/how-to-sort-python-dictionary-by-keys/






                share|improve this answer




























                  0














                  You can use key-value (dictionary) for this. Then simply sort your list according to key/value using methods given in https://www.saltycrane.com/blog/2007/09/how-to-sort-python-dictionary-by-keys/






                  share|improve this answer


























                    0












                    0








                    0







                    You can use key-value (dictionary) for this. Then simply sort your list according to key/value using methods given in https://www.saltycrane.com/blog/2007/09/how-to-sort-python-dictionary-by-keys/






                    share|improve this answer













                    You can use key-value (dictionary) for this. Then simply sort your list according to key/value using methods given in https://www.saltycrane.com/blog/2007/09/how-to-sort-python-dictionary-by-keys/







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jan 2 at 14:53









                    Sham GirSham Gir

                    11




                    11

























                        0














                        You can sort your list with the sorted and the argument key



                        l = ['har 1', 'zee 3']
                        print(sorted(l, key=lambda x: (-int(x.split()[1]), x.split()[0])))


                        Output



                        ['zee 3', 'har 1']


                        You can even print it as you wanted with the unpack operator



                        print(*sorted(l, key=lambda x: (-int(x.split()[1]), x.split()[0])), sep='n')


                        Here I am creating from each value a tuple of (-int(second_element), first_element)



                        So it will sort decreasingly by the numbers, and in case of equality, it will sort alphabetically






                        share|improve this answer




























                          0














                          You can sort your list with the sorted and the argument key



                          l = ['har 1', 'zee 3']
                          print(sorted(l, key=lambda x: (-int(x.split()[1]), x.split()[0])))


                          Output



                          ['zee 3', 'har 1']


                          You can even print it as you wanted with the unpack operator



                          print(*sorted(l, key=lambda x: (-int(x.split()[1]), x.split()[0])), sep='n')


                          Here I am creating from each value a tuple of (-int(second_element), first_element)



                          So it will sort decreasingly by the numbers, and in case of equality, it will sort alphabetically






                          share|improve this answer


























                            0












                            0








                            0







                            You can sort your list with the sorted and the argument key



                            l = ['har 1', 'zee 3']
                            print(sorted(l, key=lambda x: (-int(x.split()[1]), x.split()[0])))


                            Output



                            ['zee 3', 'har 1']


                            You can even print it as you wanted with the unpack operator



                            print(*sorted(l, key=lambda x: (-int(x.split()[1]), x.split()[0])), sep='n')


                            Here I am creating from each value a tuple of (-int(second_element), first_element)



                            So it will sort decreasingly by the numbers, and in case of equality, it will sort alphabetically






                            share|improve this answer













                            You can sort your list with the sorted and the argument key



                            l = ['har 1', 'zee 3']
                            print(sorted(l, key=lambda x: (-int(x.split()[1]), x.split()[0])))


                            Output



                            ['zee 3', 'har 1']


                            You can even print it as you wanted with the unpack operator



                            print(*sorted(l, key=lambda x: (-int(x.split()[1]), x.split()[0])), sep='n')


                            Here I am creating from each value a tuple of (-int(second_element), first_element)



                            So it will sort decreasingly by the numbers, and in case of equality, it will sort alphabetically







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Jan 2 at 14:57









                            BlueSheepTokenBlueSheepToken

                            1,826517




                            1,826517























                                0














                                You can use the key parameter of the sorted function to specify a custom sorting order -



                                l = ['X 3', 'Z 1', 'A 2']
                                l_sorted = sorted(l, key=lambda x: x.split()[-1])
                                print(l_sorted)
                                /*Returns ['Z 1', 'A 2', 'X 3']*/





                                share|improve this answer




























                                  0














                                  You can use the key parameter of the sorted function to specify a custom sorting order -



                                  l = ['X 3', 'Z 1', 'A 2']
                                  l_sorted = sorted(l, key=lambda x: x.split()[-1])
                                  print(l_sorted)
                                  /*Returns ['Z 1', 'A 2', 'X 3']*/





                                  share|improve this answer


























                                    0












                                    0








                                    0







                                    You can use the key parameter of the sorted function to specify a custom sorting order -



                                    l = ['X 3', 'Z 1', 'A 2']
                                    l_sorted = sorted(l, key=lambda x: x.split()[-1])
                                    print(l_sorted)
                                    /*Returns ['Z 1', 'A 2', 'X 3']*/





                                    share|improve this answer













                                    You can use the key parameter of the sorted function to specify a custom sorting order -



                                    l = ['X 3', 'Z 1', 'A 2']
                                    l_sorted = sorted(l, key=lambda x: x.split()[-1])
                                    print(l_sorted)
                                    /*Returns ['Z 1', 'A 2', 'X 3']*/






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Jan 2 at 14:57









                                    MortzMortz

                                    795519




                                    795519















                                        Popular posts from this blog

                                        Monofisismo

                                        Angular Downloading a file using contenturl with Basic Authentication

                                        Olmecas