How to sort a list of time values?sorting based on time a dictionary when the time is h:mm format onlyregex for detecting subtitle errorsHow to merge two dictionaries in a single expression?How do I check if a list is empty?How do I sort a list of dictionaries by a value of the dictionary?How can I make a time delay in Python?How do I sort a dictionary by value?How to make a flat list out of list of lists?Sorting an array of JavaScript objectsSort array of objects by string property valueHow to Sort a List<T> by a property in the objectHow to pair socks from a pile efficiently?

When were female captains banned from Starfleet?

Pre-mixing cryogenic fuels and using only one fuel tank

Shouldn’t conservatives embrace universal basic income?

Doesn't the system of the Supreme Court oppose justice?

Does "he squandered his car on drink" sound natural?

Quoting Keynes in a lecture

A variation to the phrase "hanging over my shoulders"

Why can't the Brexit deadlock in the UK parliament be solved with a plurality vote?

Why is the Sun approximated as a black body at ~ 5800 K?

Why does AES have exactly 10 rounds for a 128-bit key, 12 for 192 bits and 14 for a 256-bit key size?

How can ping know if my host is down

Are Captain Marvel's powers affected by Thanos breaking the Tesseract and claiming the stone?

Can I turn my anal-retentiveness into a career?

What is the highest possible scrabble score for placing a single tile

Find the next value of this number series

How could a planet have erratic days?

Biological Blimps: Propulsion

What features enable the Su-25 Frogfoot to operate with such a wide variety of fuels?

The Digit Triangles

Why do ¬, ∀ and ∃ have the same precedence?

How to convince somebody that he is fit for something else, but not this job?

Why do Radio Buttons not fill the entire outer circle?

Is there any evidence that Cleopatra and Caesarion considered fleeing to India to escape the Romans?

Is this toilet slogan correct usage of the English language?



How to sort a list of time values?


sorting based on time a dictionary when the time is h:mm format onlyregex for detecting subtitle errorsHow to merge two dictionaries in a single expression?How do I check if a list is empty?How do I sort a list of dictionaries by a value of the dictionary?How can I make a time delay in Python?How do I sort a dictionary by value?How to make a flat list out of list of lists?Sorting an array of JavaScript objectsSort array of objects by string property valueHow to Sort a List<T> by a property in the objectHow to pair socks from a pile efficiently?













7















I have a python list of time values that I extracted from a web log. I have the list in the format of %H:%M:%S. How would I sort the time values in ascending order?










share|improve this question




























    7















    I have a python list of time values that I extracted from a web log. I have the list in the format of %H:%M:%S. How would I sort the time values in ascending order?










    share|improve this question


























      7












      7








      7


      3






      I have a python list of time values that I extracted from a web log. I have the list in the format of %H:%M:%S. How would I sort the time values in ascending order?










      share|improve this question
















      I have a python list of time values that I extracted from a web log. I have the list in the format of %H:%M:%S. How would I sort the time values in ascending order?







      python list sorting time






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 8 at 0:51









      martineau

      69.3k1092186




      69.3k1092186










      asked Jul 18 '13 at 3:11









      intuitionintuition

      36115




      36115






















          4 Answers
          4






          active

          oldest

          votes


















          4














          import time

          sorted((time.strptime(d, "%H:%M:%S") for d in time_list), reverse=True)





          share|improve this answer






























            4














            Just sorted(time_list) works fine.



            >>> sorted(["14:10:01", "03:12:08"])
            ["03:12:08", "14:10:01"]





            share|improve this answer




















            • 11





              This works because the times are '0' padded.

              – SethMMorton
              Jul 18 '13 at 3:36


















            2














            sorted([tuple(map(int, d.split(":"))) for d in my_time_list])


            Where each element in my_time_list is of the form you describe, for example:



            >>> my_time_list
            ["03:12:08", "14:10:01"]





            share|improve this answer

























            • it returns [[3, 12, 8], [14, 10, 1]]. Data type of elements in returned list is different from the input list, which may not be what he whats.

              – hago
              Jul 18 '13 at 3:32












            • I often cast time units to ints because 1) You get type checking and 2) It allows easier interoperability with the datatime module types

              – Owen
              Jul 18 '13 at 3:42



















            0














            you should be able use the method sort(key=str.lower) since your time is parsed as a string






            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%2f17713873%2fhow-to-sort-a-list-of-time-values%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              4 Answers
              4






              active

              oldest

              votes








              4 Answers
              4






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              4














              import time

              sorted((time.strptime(d, "%H:%M:%S") for d in time_list), reverse=True)





              share|improve this answer



























                4














                import time

                sorted((time.strptime(d, "%H:%M:%S") for d in time_list), reverse=True)





                share|improve this answer

























                  4












                  4








                  4







                  import time

                  sorted((time.strptime(d, "%H:%M:%S") for d in time_list), reverse=True)





                  share|improve this answer













                  import time

                  sorted((time.strptime(d, "%H:%M:%S") for d in time_list), reverse=True)






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jul 18 '13 at 3:53









                  fabrizioMfabrizioM

                  31k126898




                  31k126898























                      4














                      Just sorted(time_list) works fine.



                      >>> sorted(["14:10:01", "03:12:08"])
                      ["03:12:08", "14:10:01"]





                      share|improve this answer




















                      • 11





                        This works because the times are '0' padded.

                        – SethMMorton
                        Jul 18 '13 at 3:36















                      4














                      Just sorted(time_list) works fine.



                      >>> sorted(["14:10:01", "03:12:08"])
                      ["03:12:08", "14:10:01"]





                      share|improve this answer




















                      • 11





                        This works because the times are '0' padded.

                        – SethMMorton
                        Jul 18 '13 at 3:36













                      4












                      4








                      4







                      Just sorted(time_list) works fine.



                      >>> sorted(["14:10:01", "03:12:08"])
                      ["03:12:08", "14:10:01"]





                      share|improve this answer















                      Just sorted(time_list) works fine.



                      >>> sorted(["14:10:01", "03:12:08"])
                      ["03:12:08", "14:10:01"]






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Dec 23 '15 at 19:44









                      AndreL

                      3,13022034




                      3,13022034










                      answered Jul 18 '13 at 3:34









                      hagohago

                      1,17821215




                      1,17821215







                      • 11





                        This works because the times are '0' padded.

                        – SethMMorton
                        Jul 18 '13 at 3:36












                      • 11





                        This works because the times are '0' padded.

                        – SethMMorton
                        Jul 18 '13 at 3:36







                      11




                      11





                      This works because the times are '0' padded.

                      – SethMMorton
                      Jul 18 '13 at 3:36





                      This works because the times are '0' padded.

                      – SethMMorton
                      Jul 18 '13 at 3:36











                      2














                      sorted([tuple(map(int, d.split(":"))) for d in my_time_list])


                      Where each element in my_time_list is of the form you describe, for example:



                      >>> my_time_list
                      ["03:12:08", "14:10:01"]





                      share|improve this answer

























                      • it returns [[3, 12, 8], [14, 10, 1]]. Data type of elements in returned list is different from the input list, which may not be what he whats.

                        – hago
                        Jul 18 '13 at 3:32












                      • I often cast time units to ints because 1) You get type checking and 2) It allows easier interoperability with the datatime module types

                        – Owen
                        Jul 18 '13 at 3:42
















                      2














                      sorted([tuple(map(int, d.split(":"))) for d in my_time_list])


                      Where each element in my_time_list is of the form you describe, for example:



                      >>> my_time_list
                      ["03:12:08", "14:10:01"]





                      share|improve this answer

























                      • it returns [[3, 12, 8], [14, 10, 1]]. Data type of elements in returned list is different from the input list, which may not be what he whats.

                        – hago
                        Jul 18 '13 at 3:32












                      • I often cast time units to ints because 1) You get type checking and 2) It allows easier interoperability with the datatime module types

                        – Owen
                        Jul 18 '13 at 3:42














                      2












                      2








                      2







                      sorted([tuple(map(int, d.split(":"))) for d in my_time_list])


                      Where each element in my_time_list is of the form you describe, for example:



                      >>> my_time_list
                      ["03:12:08", "14:10:01"]





                      share|improve this answer















                      sorted([tuple(map(int, d.split(":"))) for d in my_time_list])


                      Where each element in my_time_list is of the form you describe, for example:



                      >>> my_time_list
                      ["03:12:08", "14:10:01"]






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Jul 18 '13 at 3:43

























                      answered Jul 18 '13 at 3:19









                      OwenOwen

                      1,500714




                      1,500714












                      • it returns [[3, 12, 8], [14, 10, 1]]. Data type of elements in returned list is different from the input list, which may not be what he whats.

                        – hago
                        Jul 18 '13 at 3:32












                      • I often cast time units to ints because 1) You get type checking and 2) It allows easier interoperability with the datatime module types

                        – Owen
                        Jul 18 '13 at 3:42


















                      • it returns [[3, 12, 8], [14, 10, 1]]. Data type of elements in returned list is different from the input list, which may not be what he whats.

                        – hago
                        Jul 18 '13 at 3:32












                      • I often cast time units to ints because 1) You get type checking and 2) It allows easier interoperability with the datatime module types

                        – Owen
                        Jul 18 '13 at 3:42

















                      it returns [[3, 12, 8], [14, 10, 1]]. Data type of elements in returned list is different from the input list, which may not be what he whats.

                      – hago
                      Jul 18 '13 at 3:32






                      it returns [[3, 12, 8], [14, 10, 1]]. Data type of elements in returned list is different from the input list, which may not be what he whats.

                      – hago
                      Jul 18 '13 at 3:32














                      I often cast time units to ints because 1) You get type checking and 2) It allows easier interoperability with the datatime module types

                      – Owen
                      Jul 18 '13 at 3:42






                      I often cast time units to ints because 1) You get type checking and 2) It allows easier interoperability with the datatime module types

                      – Owen
                      Jul 18 '13 at 3:42












                      0














                      you should be able use the method sort(key=str.lower) since your time is parsed as a string






                      share|improve this answer



























                        0














                        you should be able use the method sort(key=str.lower) since your time is parsed as a string






                        share|improve this answer

























                          0












                          0








                          0







                          you should be able use the method sort(key=str.lower) since your time is parsed as a string






                          share|improve this answer













                          you should be able use the method sort(key=str.lower) since your time is parsed as a string







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Jul 18 '13 at 3:25









                          ConcoConco

                          363




                          363



























                              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%2f17713873%2fhow-to-sort-a-list-of-time-values%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 get text form Clipboard with JavaScript in Firefox 56?How to validate an email address in JavaScript?How do JavaScript closures work?How do I remove a property from a JavaScript object?How do you get a timestamp in JavaScript?How do I copy to the clipboard in JavaScript?How do I include a JavaScript file in another JavaScript file?Get the current URL with JavaScript?How to replace all occurrences of a string in JavaScriptHow to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?

                              Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

                              List of MPs elected to the English parliament in 1640 (April) Contents List of constituencies and members See also Notes References Navigation menueNational Archives – The Glynde Place ArchivesCobbett's Parliamentary history of England, from the Norman Conquest in 1066 to the year 1803'Aldermen in Parliament', The Aldermen of the City of London: Temp. Henry III – 1912onepage&q&f&#61, false 229