1 Replies - 12275 Views - Last Post: 28 February 2013 - 07:03 PM Rate Topic: -----

#1 Midwest Product  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 74
  • Joined: 05-February 10

Sorting a deque in Python

Posted 28 February 2013 - 05:13 PM

So I have a deque of people objects. A person contains a name and an age, that's it. I'm trying to figure out how to sort the deque on the name field.

p1 = person("Tom", 31)
p2 = person("Jason", 33)
p3 = person("Adam", 23)
p4 = person("Megan", 28)

classroom = deque()

#deque is now full of unsorted people

Any idea how to sort, just based on the name field?

This post has been edited by Midwest Product: 28 February 2013 - 05:13 PM

Is This A Good Question/Topic? 0
  • +

Replies To: Sorting a deque in Python

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2579
  • View blog
  • Posts: 4,114
  • Joined: 21-June 11

Re: Sorting a deque in Python

Posted 28 February 2013 - 07:03 PM

You can use the sorted function to get a list containing the sorted contents of a given deque (or any other iterable). If necessary you can convert that list back to a deque after sorting it.

Deques don't have a sort method, so there's no way to sort it in-place without implementing the algorithm itself. However there should be no need to do this as for linked lists - which, from the documentation, I believe deques to be implemented as (based on the fact that random access is O(n)) - in-place sorting algorithms have less of an advantage over non-in-place ones than is the case for array-backed data structures.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1