Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1024583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:47:57+00:00 2026-05-16T11:47:57+00:00

I have two instances of an object in a list class Thing(): timeTo =

  • 0

I have two instances of an object in a list

class Thing():
     timeTo = 0
     timeFrom = 0
     name = ""

o1 = Thing()
o1.name = "One"
o1.timeFrom = 2

o2 = Thing()
o2.timeTo = 20
o2.name = "Two"

myList = [o1, o2]


biggestIndex = (myList[0].timeFrom < myList[1].timeTo) & 1
bigger  = myList.pop(biggestIndex)
lesser  = myList.pop()

print bigger.name
print lesser.name

both o1 and o2 have two properties that I want to compare the first in the lists timeFrom property and the second ones timeTo property to eachother.

I feel this is a bit awkward and wierd, is there perhaps a better and more readable approach to this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T11:47:58+00:00Added an answer on May 16, 2026 at 11:47 am

    The best solution is to make Thing instances sortable. You do this by implementing __lt__:

    class Thing():
         timeTo = 0
         timeFrom = 0
         name = ""
    
         def __lt__(self, other):
             return self.timeFrom < other.timeTo
    
    
    lesser, bigger = sorted(myList)
    

    Python2 has lesser, bigger = sorted(myList, cmp=lambda one,other: one.timeFrom < other.timeTo).

    In Python3 cmp is gone, I guess to force people to do (or learn) OOP and write a adapter.

    class SortAdaper(object):
        def __init__(self, obj ):
            self.obj = obj
    
    class TimeLineSorter(SortAdaper):
        """ sorts in a timeline """
        def __lt__(self, other):
            return self.obj.timeFrom < other.obj.timeTo
    
    class NameSorter(SortAdaper):
        """ sorts by name """
        def __lt__(self, other):
            return self.obj.name < other.obj.name
    
    print sorted( myList, key=TimeLineSorter)
    print sorted( myList, key=NameSorter)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a List object in my class and two methods which are used
Is it possible for two instances of Object to have the same hashCode() ?
I have a class, and list of instances, that looks something like this (field
If I have two objects, one being the list of items, and the other
I have two WPF list boxes. One is a list of lists (actually a
I have a manager class maintaining a list of objects. Each Object has a
I have two instances of an Address.ascx control in an ASP.NET MVC page. <h1>Shipping
I have two instances of CFMutableBitVector , like so: CFBitVectorRef ref1, ref2; How can
I have two Java instances of java.util.Date and I have to find out if
Say I have two UUID instances: uuid1 = UUID.randomUUID(); uuid2 = UUID.randomUUID(); If those

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.