Thread: Help with sorting a ListCtrl

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    552

    Help with sorting a ListCtrl

    MSDN states that the lParam1 and lParam2 of the callback procedure will be the "item data" (i wont even go into how useless in many cases that param will be when trying to sort insorting), but in their example they assume lParam1 and lParam2 will be the index of the items to be compared.

    http://msdn.microsoft.com/library/de....sortitems.asp

    I need to get the label for each item (plus some other stuff) to be able to compare them. The only way I can see to do this is to dynamically allocate a struct with all the necessary values and use that as the item data. But then I end up having to write my own list deletion routine to make sure everything gets deallocated.

    Am I misunderstaning something about the MSDN description? If not, is dynamic allocation the only way there is to acccomplish what im trying to do?
    C Code. C Code Run. Run Code Run... Please!

    "Love is like a blackhole, you fall into it... then you get ripped apart"

  2. #2
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,227
    :: From what you're saying I get two things..

    1) You want to sort a listbox - try giving the LBS_SORT flag to it.
    2) You want to get the text of the items in the listbox. Try LB_GETSEL

    Tell me if I misunderstand what you're asking...

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    The problem isnt getting the list ctrl to attempt a sort, or getting an items text, its trying to do both of those at the same time(!) To implement the sort, you have to write a callback procedure, but the parameters you get in the callback procedure are limited. The only parameters you get are the "item data" (the lParam http://msdn.microsoft.com/library/de...insertitem.asp) for the two items needed to be compared, plus one other value with is usually a pointer to the ListCtrl.

    So, the question is how to I get each items text with only the "item data" and a pointer to the ListCtrl to work with? You cant, unless i put some info in the item data that will allow me to reference the item's index or something, this would require dynamic mem allocation.
    C Code. C Code Run. Run Code Run... Please!

    "Love is like a blackhole, you fall into it... then you get ripped apart"

  4. #4
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    LB_GETTEXT.

    I can't believe you could read MSDN and not find that. Try searching MSDN to find out how to use this message. As it appears you do not know how to implement a search, here's some instructions:

    1. Go to search engine
    2. In the edit box, type the topic you wish to find information about
    3. Press go
    4. Read the articles

    Good luck, you're gonna need it.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    HEY IDIOT, make sure you bother to read my post before you act condescending towards me! I said LISTCTRL not LISTBOX. And if you had bothered to read, you would understand there are further complications than just getting the text.
    C Code. C Code Run. Run Code Run... Please!

    "Love is like a blackhole, you fall into it... then you get ripped apart"

  6. #6
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    As far as I know, list control means list box. Plus, -Ken- also assumed that you meant list box, yet you failed to correct him. I think I am justified here.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  7. #7
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    LVM_GETITEM

    Believe it or not, I found that while searching MSDN. Send the message to the list control, specifying the index in the iItem member of the LV_ITEM structure.

    I believe list views and list controls share the same messages and styles, so I hope I'm in line here.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  8. #8
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Any more bitterness and this thread is trashed

  9. #9
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    >Ken- also assumed that you meant list box, yet you failed to correct him. I think I am justified here.

    I didnt correct him directly, but I clarified my question in my second post.

    I dont think condescending responses are ever justified (except maybe in extreme cases). That is a big problem with some of the people on this board.

    >Send the message to the list control, specifying the index in the iItem member of the LV_ITEM structure

    That was the problem I was having. No index is given and it is impossible (afaik) to retrieve the index directly from the parameters that are given to the callback procedure. The only way I could think of was to use dynamic allocation to store all of the necessary info.

    My question was is there a way to accomplish this (getting the text, given the limitations of the callback parameters) without using dynamic mem alloc. Perhaps I didnt make that clear.
    C Code. C Code Run. Run Code Run... Please!

    "Love is like a blackhole, you fall into it... then you get ripped apart"

  10. #10
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Ok, let's have a truce, and get to the bottom of this problem. It is a bit of a problem not getting the item index... but if the item data's all that's given to you, I don't see how you can avoid using it. Tell me more about the sorting routine, ie, how do you commence it, what parameters do you give it, etc. I'm thinking, maybe you can just make your own routine.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  11. #11
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    >Ok, let's have a truce
    agreed

    The sorting routine is implemented by the ListCtrl itself. What we are supposed to define is the callback routine to compare two items. Im writing a file explorer program, so I need to be able to sort by various things (ie. name, attributes, size, date modified).

    the protoype for the callback routine is:
    int CALLBACK CompareFunc(LPARAM lParam1, LPARAM lParam2,
    LPARAM lParamSort);

    where lParam1 and 2 are the item data, and lParamSort is the value that I send as a parameter when I call the SortItems member function. The only reasonable choice for that param is a pointer to the ListCtrl since none is given otherwise.

    I thought of implementing my own sort, but I figured I should use that as a last resort since other complications will arise when the user manipulates the ListCtrl.

    What I finally ended up doing was using the finddata struct associated with the findfirst/next functions and storing a dyn. allocated copy in the item data.
    C Code. C Code Run. Run Code Run... Please!

    "Love is like a blackhole, you fall into it... then you get ripped apart"

  12. #12
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    I still can't believe Microsoft would do that. I can't think of any efficient way to utilize the item data, the only use is like you say, an array that contains the indexes. I vote we invent a new control, one that people can actually use.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  13. #13
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>is dynamic allocation the only way there is to acccomplish what im trying to do?<<

    It's a while since last I looked at this stuff (from api, not mfc angle) but that was pretty much how I recall doing it: cast the two passed parameters to pointers to your data struct/class and return the results of the comparison of suitable members of those structs/classes.

    ...and judging by that msdn example, looks like that's the 'recommended' way to go...which means there probably is a better way to do it.

    Good luck anyway.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 06-11-2009, 11:27 AM
  2. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  3. sorting structure members using pointers
    By robstr12 in forum C Programming
    Replies: 5
    Last Post: 07-25-2005, 05:50 PM
  4. Replies: 2
    Last Post: 02-23-2004, 06:34 AM
  5. Still Needing Help : selection sorting
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 10-14-2001, 08:41 PM