! * Clears all values in the specified columns. * @param cols The columns in the spreadsheet to clear. */
| 754 | * @param cols The columns in the spreadsheet to clear. |
| 755 | */ |
| 756 | void Spreadsheet::sortColumns(Column* leading, const QVector<Column*>& cols, bool ascending) { |
| 757 | DEBUG(Q_FUNC_INFO << ", ascending = " << ascending) |
| 758 | if (cols.isEmpty()) |
| 759 | return; |
| 760 | |
| 761 | // the normal QPair comparison does not work properly with descending sorting |
| 762 | // therefore we use our own compare functions |
| 763 | // TODO: check this. a < b vs. a.first < b.first |
| 764 | class CompareFunctions { |
| 765 | public: |
| 766 | static bool doubleLess(QPair<double, int> a, QPair<double, int> b) { |
| 767 | return a.first < b.first; |
| 768 | } |
| 769 | static bool doubleGreater(QPair<double, int> a, QPair<double, int> b) { |
| 770 | return a.first > b.first; |
| 771 | } |
| 772 | static bool integerLess(QPair<int, int> a, QPair<int, int> b) { |
| 773 | return a.first < b.first; |
| 774 | } |
| 775 | static bool integerGreater(QPair<int, int> a, QPair<int, int> b) { |
| 776 | return a.first > b.first; |
| 777 | } |
| 778 | static bool bigIntLess(QPair<qint64, int> a, QPair<qint64, int> b) { |
| 779 | return a.first < b.first; |
| 780 | } |
| 781 | static bool bigIntGreater(QPair<qint64, int> a, QPair<qint64, int> b) { |
| 782 | return a.first > b.first; |
| 783 | } |
| 784 | static bool QStringLess(const QPair<QString, int>& a, const QPair<QString, int>& b) { |
| 785 | return a < b; |
| 786 | } |
| 787 | static bool QStringGreater(const QPair<QString, int>& a, const QPair<QString, int>& b) { |
| 788 | return a > b; |
| 789 | } |
| 790 | static bool QDateTimeLess(const QPair<QDateTime, int>& a, const QPair<QDateTime, int>& b) { |
| 791 | return a < b; |
| 792 | } |
| 793 | static bool QDateTimeGreater(const QPair<QDateTime, int>& a, const QPair<QDateTime, int>& b) { |
| 794 | return a > b; |
| 795 | } |
| 796 | }; |
| 797 | |
| 798 | WAIT_CURSOR; |
| 799 | beginMacro(i18n("%1: sort columns", name())); |
| 800 | |
| 801 | if (!leading) { // sort separately |
| 802 | DEBUG(" sort separately") |
| 803 | for (auto* col : cols) { |
| 804 | int rows = col->rowCount(); |
| 805 | std::unique_ptr<Column> tempCol(new Column(QStringLiteral("temp"), col->columnMode())); |
| 806 | |
| 807 | switch (col->columnMode()) { |
| 808 | case AbstractColumn::ColumnMode::Double: { |
| 809 | QVector<QPair<double, int>> map; |
| 810 | |
| 811 | for (int i = 0; i < rows; i++) |
| 812 | if (col->isValid(i)) |
| 813 | map.append(QPair<double, int>(col->valueAt(i), i)); |
nothing calls this directly
no test coverage detected