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 4070646
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T16:39:15+00:00 2026-05-20T16:39:15+00:00

I have a model with an attribute which keeps track of the price. Right

  • 0

I have a model with an attribute which keeps track of the price. Right now, I have a list of that certain model. Is there anyway to rearrange the list to sort by that particular attribute? Is python smart enough to know that the attribute is a value which can be sorted? I am not keeping keeping track of the instances of a particular model using a database (it is not needed for what I am doing, so I cannot just retrieve the instances from the database in sorted order)
Thanks!

  • 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-20T16:39:16+00:00Added an answer on May 20, 2026 at 4:39 pm

    You can use the inbuilt sorted function, together with a custom-made function that returns the price of an object:

    class Dummy(object) :
        pass
    
    def getPrice(obj) :
        return obj.price
    
    d0 = Dummy()
    d0.price = 56.
    d1 = Dummy()
    d1.price=16.
    
    d2 = Dummy()
    d2.price=786.
    
    d3 = Dummy()
    d3.price=5.5
    
    elements = [d0, d1, d2, d3]
    
    print 'Pre-sorting:'
    for elem in elements :
        print elem.price
    
    sortedElements = sorted(elements, key=getPrice)
    
    print 'Post-sorting:'
    for elem in sortedElements :
        print elem.price
    

    This would also work via any method of your class that returns the price, e.g.

    class Dummy(object) :
        def __init__(self, price) :
            self._price = price
        def getPrice(self) :
            return self._price
    
    ...
    
    sortedElements = sorted(elements, key = Dummy.getPrice)
    

    See http://wiki.python.org/moin/HowTo/Sorting/ for more.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a model called a voip_phone that has a an attribute schedule which
I've been wondering, suppose I have a model with an attribute that in every
[Updated a bit] I have a Task model that has a :completed boolean attribute.
I have a model which contains a ManyToMany to User to keep track of
I have model TeacherLeader which keeps foreign keys for other tables. I can update
I have a model in which I specify certain fields. class Upload_files(models.Model): title =models.CharField(max_length=100,
I have an ActiveRecord Appointment model, which includes topic and time attributes. I would
I want to turn Model.attribute into Attribute.models. e.g. I currently have Model.first.attribute => string
I have a model tag with attribute title (string) . A tag belongs_to a
I have a model with a virtual attribute for a time interval: attr_accessible :description,

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.