std::sort and listview_sortitems

This is a discussion on std::sort and listview_sortitems within the Windows Programming forums, part of the Platform Specific Boards category; Ok, i have a list view control that holds data like, artist names, music names, etc... and i have a ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Location
    Brasil
    Posts
    220

    std::sort and listview_sortitems

    Ok, i have a list view control that holds data like, artist names, music names, etc... and i have a std::vector<std::string>, that holds all the paths for the files that have X artist name or X music name. When, i sort the listview, std::vector canīt sort itself. So i tryed std::sort, but when i did that, i realized that i was sorting the name of the paths, not the name of the paths according (donīt know how to write this in english) to their respective artist names or music names. What could i do?

    Code:
    int CALLBACK ListViewCompareProc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
    {
    	cString String;
    	static WCHAR szBuf1[50], szBuf2[50];
     
    	ListView_GetItemText(Cont.ContainerhWnd, lParam1, lParamSort, szBuf1, sizeof(szBuf1));
    	ListView_GetItemText(Cont.ContainerhWnd, lParam2, lParamSort, szBuf2, sizeof(szBuf2));
    
    	return(strcmp(String.ToMultibyte(szBuf1, CP_ACP,0).c_str(), String.ToMultibyte(szBuf2, CP_ACP,0).c_str()));//DESCENDING ORDER
    }
    Thank you

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Firstly, ListView_SortItemsEx must be used rather than ListView_SortItems.

    And the function can look something like:
    Code:
    #include <wchar.h> /* Need this for wcscmp, could also use lstrcmp from windows.h */
    
    int CALLBACK ListViewCompareProc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
    {
    	WCHAR szBuf1[50], szBuf2[50]; /* No need for static */
     
    	/* Get the text of the two items. Note that buffer size is in characters, not bytes. */
    	ListView_GetItemText(Cont.ContainerhWnd, lParam1, lParamSort, szBuf1, sizeof(szBuf1) / sizeof(szBuf1[0]);
    	ListView_GetItemText(Cont.ContainerhWnd, lParam2, lParamSort, szBuf2, sizeof(szBuf2) / sizeof(szBuf2[0]);
    
    	return wcscmp(szBuf1, szBuf2);//DESCENDING ORDER
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. std::sort with memory?
    By cyberfish in forum C++ Programming
    Replies: 21
    Last Post: 02-22-2008, 02:03 PM
  2. swap()? (g++)
    By jafet in forum C++ Programming
    Replies: 4
    Last Post: 08-05-2006, 05:59 PM
  3. Problem: std::sort() function use
    By justdoit22 in forum C++ Programming
    Replies: 13
    Last Post: 01-14-2004, 01:42 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21