Quote Originally Posted by vinnu View Post
u can understand my code ...
Unreadable code is a bad code.

First let's see how many lines of code it would take to implement it using QListViewItem::compare():
Qt Code:
  1. int CustomListViewItem::compare( QListViewItem * item, int col, bool ascending ) const
  2. {
  3. Q_UNUSED( ascending ); // ignored, like the docs recommend
  4. if( col == 0 ) {
  5. int a = text( col ).toInt();
  6. int b = item->text( col ).toInt();
  7. return (a - b);
  8. }
  9. else {
  10. return QString::localeAwareCompare( text( col ), item->text( col ) );
  11. }
  12. }
To copy to clipboard, switch view to plain text mode 
See? It wasn't that hard.

Now for the sortColumns(). First of all you use QString::latin1(). What will happen with non-ASCII characters? And why do you delete all items, if you just need to reorder them? Why don't you reuse existing code?

I would do it this way:
Qt Code:
  1. class NumericalComparator
  2. {
  3. public:
  4. NumericalComparator( int column ) : _column( column ) {}
  5.  
  6. bool operator()( const QListViewItem *a, const QListViewItem *b ) const
  7. {
  8. return ( ( a->text( _column ).toInt() - b->text( _column ).toInt() ) > 0 );
  9. }
  10.  
  11. private:
  12. int _column;
  13. };
  14.  
  15. class StringComparator
  16. {
  17. public:
  18. StringComparator( int column ) : _column( column ) {}
  19.  
  20. bool operator()( const QListViewItem *a, const QListViewItem *b ) const
  21. {
  22. return ( QString::localeAwareCompare( a->text( _column ), b->text( _column ) ) > 0 );
  23. }
  24.  
  25. private:
  26. int _column;
  27. };
  28.  
  29. void BBPickerDialog::sortColumns(int index)
  30. {
  31. m_pBBListView->setUpdatesEnabled( false );
  32.  
  33. std::vector< QListViewItem * > items( m_pBBListView->childCount() );
  34.  
  35. for( QListViewItemIterator it( m_pBBListView ); it.current(); ++it ) {
  36. items.push_back( it.current() );
  37. }
  38.  
  39. if( index == 0 ) {
  40. std::sort( items.begin(), items.end(), NumericalComparator( index ) );
  41. }
  42. else {
  43. std::sort( items.begin(), items.end(), StringComparator( index ) );
  44. }
  45.  
  46. for( std::vector< QListViewItem *>::iterator it = items.begin(); it != items.end(); ++it ) {
  47. m_pBBListView->takeItem( *it );
  48. m_pBBListView->insertItem( *it );
  49. }
  50.  
  51. m_pBBListView->setUpdatesEnabled( true );
  52. }
To copy to clipboard, switch view to plain text mode 
Now (provided that this code works, as I didn't test it) you can see that you don't need to copy anything, except for the pointers, and that you can easily reuse existing code (in this case from STL).

If you don't like STL, you can do a similar thing using QPtrList --- just subclass QPtrList<QListViewItem> and reimplement compareItems().