sort(int column, bool ascending, int sortType) sortType 0:Numerical, 1:String, 2:String (ignore case) – default: 1 */
| 116 | sortType 0:Numerical, 1:String, 2:String (ignore case) – default: 1 |
| 117 | */ |
| 118 | void CsvDataStorage::sort(table_index_t column, bool ascending, int sortType) { |
| 119 | |
| 120 | if( rows() <= 1 || column < 0 || column >= columns() ) { |
| 121 | return; |
| 122 | } |
| 123 | |
| 124 | long counter = 0; |
| 125 | std::sort( tableData.begin(), tableData.end(), [&column, &ascending, &sortType, &counter](const auto& lhs, const auto& rhs) { |
| 126 | std::string s1 = getColumn(lhs, column); |
| 127 | std::string s2 = getColumn(rhs, column); |
| 128 | double d1, d2; |
| 129 | std::string lowerS1; |
| 130 | std::string lowerS2; |
| 131 | if( counter % 50000 == 0 ) { |
| 132 | Fl::check(); |
| 133 | } |
| 134 | ++counter; |
| 135 | switch( sortType ) { |
| 136 | case 0: // NUMERIC |
| 137 | // TODO make this optional |
| 138 | // std::replace( s1.begin(), s1.end(), ',', '.'); |
| 139 | // std::replace( s2.begin(), s2.end(), ',', '.'); |
| 140 | try { |
| 141 | d1 = std::stod(s1); |
| 142 | } catch(const std::exception&) { |
| 143 | d1 = 0; |
| 144 | } |
| 145 | try { |
| 146 | d2 = std::stod(s2); |
| 147 | } catch(const std::exception&) { |
| 148 | d2 = 0; |
| 149 | } |
| 150 | if( ascending ) { |
| 151 | return d1 < d2; |
| 152 | } else { |
| 153 | return d1 > d2; |
| 154 | } |
| 155 | break; |
| 156 | case 1: // STRING with case |
| 157 | if( ascending ) |
| 158 | return s1 < s2; |
| 159 | else |
| 160 | return s1 > s2; |
| 161 | break; |
| 162 | case 2: // STRING ignore case |
| 163 | lowerS1 = Utf8CppUtils::utf8::casefold(s1); |
| 164 | lowerS2 = Utf8CppUtils::utf8::casefold(s2); |
| 165 | if( ascending ) |
| 166 | return lowerS1 < lowerS2; |
| 167 | else |
| 168 | return lowerS1 > lowerS2; |
| 169 | break; |
| 170 | default: |
| 171 | return true; |
| 172 | } |
| 173 | }); |
| 174 | |
| 175 |
no test coverage detected