Don't forget that sort() takes a key argument to sort by an arbitrary function of the elements, so there's no need to build extra datastructures:
object_list = Word.objects.all().order_by('-product_count')[:100]
object_list.sort(key=lambda w: w.title)

--Ned.

DavidA wrote:
Tom Smith wrote:
  
With about a million records I can't imagine doing it any other way...

I want to find my top 100 words (by product_count) then sort them
alphabetically.... to make a tag cloud...

thanks...
    

If you are only dealing with 100 records after the query, then its not
a big deal to just sort them by some property in memory. Something
like:

# get the top 100 objects by the first criteria
object_list = Word.objects.all().order_by('-product_count')[:100]

# create a dict of the second criteria -> index of the current list
index = dict([(word.value, i) for i, word in enumerate(object_list)])

# get a new list of objects ordered by the second criteria
sorted_object_list = [object_list[index[key]] for key in
sorted(index.keys())]

I think I got all that syntax right...
-Dave





.

  

-- 
Ned Batchelder, http://nedbatchelder.com

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Reply via email to