1

I define a struct by class like this:

  class image:
  def __init__(self):
      self.address = ''
      self.label = 1
      self.storage = 1

Then, I put 100 stuct data into one list.

images = []
for i in range(100):
    single_image = image()
    single_image.address = 'xxx'
    single_image.label = 3 #1:5
    single_image.storage = 10 #1:100

How can I sort images by the storage?

1
  • This isn't a struct, is just a simple class. Commented Sep 25, 2018 at 3:15

2 Answers 2

4

Use operator

import operator
images.sort(key=operator.attrgetter('storage'))
Sign up to request clarification or add additional context in comments.

Comments

1
import operator  
images_sorted = sorted(images,key=operator.attrgetter('storage'))

or you can sort images in place.

images.sort(images,key=operator.attrgetter('storage'))

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.