Problem on sorting items on listviewctrl Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
rrcn
Earned a small fee
Earned a small fee
Posts: 14
Joined: Mon Nov 17, 2008 4:57 pm

Problem on sorting items on listviewctrl

Post by rrcn » Mon Jan 05, 2009 5:35 pm

Hi!

I'm having problem sorting a listview items.
The function responding the event is this:

Code: Select all

void ListViewResource::OnResourceListColClick(wxListEvent& event) {
	_lastColClicked = event.GetColumn();
    SortItems(MyCompareFunction, (long) this);
}
Not a problem with it.
The problem is that the comparing function:

Code: Select all

int wxCALLBACK MyCompareFunction(long item1, long item2, long sortData)
is called with item1 and item2 both with value 0 although I had two test items. So I should be having one call with value 0 and value 1 and not this one call. :?:

What can be the problem?[/code][/quote]

rrcn
Earned a small fee
Earned a small fee
Posts: 14
Joined: Mon Nov 17, 2008 4:57 pm

Please help

Post by rrcn » Tue Jan 06, 2009 4:09 pm

Arghh!
I really need help here!
My comparison function only receives the 0 value for the two items to be compared and I don't have a clue why!
m_count have value 2 so I don't know why it doesn't compares with second item.

Here is the code:
ListViewResource.h

Code: Select all

#ifndef LISTVIEWRESOURCE_H
#define LISTVIEWRESOURCE_H

#include <wx/listctrl.h>

const short int notOrdered = 0;
const short int crescent = 1;
const short int decreasing = 2;

const int ID_LISTVIEW1 = 101;

int wxCALLBACK MyCompareFunction(long item1, long item2, long sortData);

class ListViewResource : public wxListView {
public:
	ListViewResource(wxWindow *parent,wxWindowID winid) : wxListView(parent,winid,wxDefaultPosition,wxDefaultSize,wxLC_REPORT) {
	_lastColClicked = 0;
	_currentCol = 0;
	_order = notOrdered;
	}
	void OnResourceListColClick(wxListEvent& event);
	void setCurrentCol(short int col) {
		_currentCol = col;
	}
	short int getCurrentCol() {
		return _currentCol;
	}
	void setOrder(unsigned short int order) {
		_order = order;
	}
	unsigned short int getOrder() {
		return _order;
	}
	unsigned short int getLastColClicked() {
		return _lastColClicked;
	}

protected:
	short int _lastColClicked;
	short int _currentCol;
	unsigned short int _order;
	DECLARE_EVENT_TABLE()
};

#endif
ListViewResource.cpp

Code: Select all

#include "ListViewResource.h"

BEGIN_EVENT_TABLE(ListViewResource, wxListView)
	EVT_LIST_COL_CLICK( ID_LISTVIEW1, ListViewResource::OnResourceListColClick )
END_EVENT_TABLE()


int wxCALLBACK MyCompareFunction(long item1, long item2, long sortData)
{
    ListViewResource *lvr = (ListViewResource*) sortData;

    int col = lvr->getLastColClicked();
    long itemId1 = lvr->FindItem(-1, item1);
    long itemId2 = lvr->FindItem(-1, item2);

    wxListItem listItem1, listItem2;

    listItem1.SetId(itemId1);
    listItem1.SetColumn(col);
    listItem1.SetMask(wxLIST_MASK_TEXT);
    lvr->GetItem(listItem1);

    listItem2.SetId(itemId2);
    listItem2.SetColumn(col);
    listItem2.SetMask(wxLIST_MASK_TEXT);
    lvr->GetItem(listItem2);

    double a,b;
    listItem1.GetText().ToDouble(&a);
    listItem2.GetText().ToDouble(&b);

    switch (listItem1.GetAlign())
    {
        case wxLIST_FORMAT_LEFT:    //these are enums defined elsewhere
            //process alphabetically
            if (listItem1.GetText() < listItem2.GetText()) return -1;
            if (listItem1.GetText() > listItem2.GetText()) return 1;
            break;
        case wxLIST_FORMAT_RIGHT:
            //process numerically
            if (a < b) return -1;
            if (a > b) return 1;
            break;
		default:
			if (listItem1.GetText() < listItem2.GetText()) return -1;
            if (listItem1.GetText() > listItem2.GetText()) return 1;
            break;
    }
    return 0;
}

void ListViewResource::OnResourceListColClick(wxListEvent& event) {
	_lastColClicked = event.GetColumn();
    SortItems(MyCompareFunction, (long) this);
}

Post Reply