PDA

View Full Version : how to SORT table of numbers with QT



tommy
29th May 2009, 08:10
I have a table (rows and columns with integers) and I'd like to sort the rows lexicographically (the rows that start with smaller numbers should be first).

Does Qt have a function for it? I was thinking that maybe I could turn rows of the table into strings and then put all the strings into a QStringList. Then I could sort the strings using string comparison and then unravel the string contents and put them back in the original table. Does this sound doable or does Qt offer a better method for my problem?

Thanks!

Ginsengelf
29th May 2009, 09:06
Hi, does QTableWidget::sortItems() do what you want?

Ginsengelf

faldzip
29th May 2009, 09:19
maybe qSort() with custom LessThan function can do that.
As I understand you right, you want to sort by column 1, but if there is same value in different rows, then you compare values in column 2 and so on? What is the data structure in which you keep your data?

tommy
29th May 2009, 09:31
Thanks



void QTableWidget::sortItems ( int column, Qt::SortOrder order = Qt::AscendingOrder )


The above function indeed sorts items. I was a bit unclear about whether it sorts rows based on columns or vice versa? It can only sort the rows one column at time which means that the table has the be sorted once for each column (very many times). Isn't this a bit slow? If rows could be converted into strings then rows could be sorted as it they were simple words (although they consis of numbers) using string coparison and the result could be a bit faster? Or am I wrong?

Thanks for any ideas you might have!

tommy
29th May 2009, 09:34
Hi Faldzip,

You are exactly right about what I need to do. My data structure is currently a simple 2D array. I thought about qsort(), too, but it gets pretty complicated because the qsort() is not designed to drag along other data that it is not directly sorting.

Thanks