Thread: Sorting ListView columns

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

    Sorting ListView columns

    How can i sort a ListView column by the alphabetical order?

    thank you

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Create it with the LVS_SORTASCENDING style.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Location
    Brasil
    Posts
    220
    It isnīt working, do i have to especifically create the list view with this style or can i change it during runtime?

  4. #4
    Registered User
    Join Date
    Oct 2005
    Location
    Brasil
    Posts
    220
    it isnīt working even when i create the list view with the LVS_ASCENDING control. I put the items in the list view during run-time, i think thats the problem. Any other sugestions?

  5. #5
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Create a minimal example that replicates the problem and post the code.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  6. #6
    Registered User
    Join Date
    Oct 2005
    Location
    Brasil
    Posts
    220
    Code:
    ...
    
    int allocate = NumberLetters > (sizeof(WORD)/sizeof(WCHAR)) ? NumberLetters : (sizeof(WORD)/sizeof(WCHAR)) ;
    						
    	        WCHAR * buffer = new WCHAR[allocate];
    		*((LPWORD)buffer ) = allocate;
    		SendDlgItemMessage( hwndDlg, EDIT_BOX, EM_GETLINE, (WPARAM) 0, (LPARAM) buffer );
    		std::basic_string<WCHAR> FilePath(buffer, NumberLetters);
    
    		Player.AddMediaToLibrary( FilePath, hwndDlg ); 
    
    ...
    
    int cMusicPlayer::AddMediaToLibrary(std::basic_string<WCHAR> Path, HWND hWndListView)
    {
    	cUserInterface UI;
    	cString String;
    
    	DWORD Result = GetFileAttributes(Path.c_str());
    	WIN32_FIND_DATA FindFileData;
    	HANDLE hFind = INVALID_HANDLE_VALUE;
    	std::basic_string<WCHAR> SearchPath = Path;
    	SearchPath += L"\\*.mp3";
    	hFind = FindFirstFile( SearchPath.c_str(), &FindFileData );
    
    	if (hFind == INVALID_HANDLE_VALUE)
    	{
    		MessageBox(NULL, String.GetString(IDS_NO_FILES).c_str(), L"Error", MB_ICONERROR);
    	}
    	else
    	{
    		Path += L"\\";
    
    		UI.AddListViewItems( Path, FindFileData.cFileName, GetDlgItem(hWndListView, ID_LISTVIEW) );
    	}
    		
    	while (FindNextFile(hFind, &FindFileData) != 0)
    	{
    		UI.AddListViewItems( Path, FindFileData.cFileName, GetDlgItem(hWndListView, ID_LISTVIEW) );	
    	}
    
    ...
    
    BOOL cUserInterface::AddListViewItems(std::basic_string<WCHAR> FilePath, std::basic_string<WCHAR> FileName, HWND hWndListView)
    {
    	cString String;
    	MP3FileInfo mp3fi;
    
    	FilePath += FileName;
    	mp3fi.Init( String.ToMultibyte(FilePath,CP_ACP,0).c_str() );
    	
    	std::basic_string<WCHAR> artist;
    	std::basic_string<WCHAR> album;
    	std::basic_string<WCHAR> title;
    	
    	if (mp3fi.szArtist != NULL)
    		artist = String.ToWideChar(mp3fi.szArtist, CP_ACP,0);
    	else 
    		artist = String.GetString( IDS_UNKNOWN );
    	if (mp3fi.szAlbum != NULL)
    		album = String.ToWideChar(mp3fi.szAlbum, CP_ACP,0);
    	else 
    		album = String.GetString( IDS_UNKNOWN );
    	if (mp3fi.szTitle != NULL)
    		title = String.ToWideChar(mp3fi.szTitle, CP_ACP,0);
    	else 
    		title = String.GetString( IDS_UNKNOWN );
    
    	if ( FindListViewString( hWndListView, title ) == TRUE && FindListViewString( hWndListView, title ) == TRUE )
    		return true;
    
    	LVITEM   lv  = { 0 };
    	
    	lv.iItem = ItemCount;
        ListView_InsertItem( hWndListView, &lv);
    
    	ListView_SetItemText( hWndListView, ItemCount, 0, const_cast<LPWSTR>(title.c_str()) );
    	ListView_SetItemText( hWndListView, ItemCount, 1, const_cast<LPWSTR>(artist.c_str()) );
    	ListView_SetItemText( hWndListView, ItemCount, 2, const_cast<LPWSTR>(album.c_str()) );
    	ListView_SetItemText( hWndListView, ItemCount, 3, const_cast<LPWSTR>(FilePath.c_str()) );
        
    	ListView_SetCheckState( hWndListView, ItemCount, TRUE);
    
    	ItemCount++;
    
    	return true;
    }
    Im trying to sort any column that i have in the list view (artist, title, album and path) in the alphabetical order.

  7. #7
    Registered User
    Join Date
    Oct 2005
    Location
    Brasil
    Posts
    220
    Any clue?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Troubles with ListView and Toolbar
    By cornholio in forum Windows Programming
    Replies: 8
    Last Post: 11-14-2005, 01:26 AM
  2. ListView Sorting Problem
    By tyouk in forum Windows Programming
    Replies: 2
    Last Post: 01-09-2005, 11:32 AM
  3. Listview sorting...Can't make it work!
    By knutso in forum Windows Programming
    Replies: 4
    Last Post: 10-05-2003, 12:48 PM
  4. Listview??
    By SuperNewbie in forum C# Programming
    Replies: 4
    Last Post: 02-13-2003, 03:34 AM
  5. ListView Sorting
    By Unregistered in forum Windows Programming
    Replies: 2
    Last Post: 06-10-2002, 05:49 PM