Thread: Sorting items in a std::list

  1. #1
    alkis_y3k
    Guest

    Question Sorting items in a std::list

    Suppose I have an std::list with several values.

    How do I sort them from the smallest to the largest and vice versa? Inside the list. So for example, before soring, my list would have:
    2
    5
    1
    7
    4

    And afterwards, they are sorted:
    1
    2
    4
    5
    7

    I know about the qsort algorithm, but can someone tell me how to actually use it on a list?

    Thanks

  2. #2
    Registered User
    Join Date
    Sep 2002
    Posts
    272
    >I know about the qsort algorithm, but can someone tell me how to actually use it on a list?<

    You don't (it wouldn't work). std::list has its own sorting algorithm - std::list::sort. However, if sorting plays a large role in whatever you're doing, you may want to look at an alternative data structure.
    Joe

  3. #3
    alkis_y3k
    Guest
    Yes, sorting must be fast since I plan to use it in my 3d engine to sort objects by their depth and render them from back to front.

    What do you suggest? My object class has a getposition() function, so I can get the Z from there, I should sort the objects relative to their Z value. What would you do?

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    272
    Sorry don't know much about graphics programming (aside from using the hardware z-buffer). You could take a look at -

    http://www.google.com/search?hl=en&i...ffer+algorithm

    Or ask on the game programming forum.
    Joe

  5. #5
    alkis_y3k
    Guest
    My question is related with graphics programming, but it is just a sorting question.

    Would list::sort work for sorting the values of the list?

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    272
    >Would list::sort work for sorting the values of the list?<

    Yes. But how often are you sorting? If it is often you will be much better off using a array/vector or some form of (associative) tree.
    Joe

  7. #7
    alkis_y3k
    Guest
    I said it is in the game loop, so really often. I could osrt whever the object's Z is changed though, that would be much more efficient.

  8. #8
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    I think, for short lists like yours, the built in sort will be good enough.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  2. HELP!!!!emergency Problem~expert please help
    By unknowppl in forum C++ Programming
    Replies: 9
    Last Post: 08-21-2008, 06:41 PM
  3. HELP!!!!emergency ~expert please help
    By unknowppl in forum C Programming
    Replies: 1
    Last Post: 08-19-2008, 07:35 AM
  4. sorting list view items
    By cppdude in forum Windows Programming
    Replies: 2
    Last Post: 06-07-2002, 08:21 AM
  5. CListView & Appending Items :: MFC
    By kuphryn in forum Windows Programming
    Replies: 0
    Last Post: 04-28-2002, 11:37 AM