sort method
Sorts the matrix in ascending or descending order.
ascending
: If true, sorts the matrix in ascending order; if false, sorts it in descending order (default is true).
Returns a new matrix with sorted values.
Example:
var matrix = Matrix([[3], [1], [4]]);
var sortedMatrix = matrix.sort(ascending: false);
print(sortedMatrix);
// Output:
// 4
// 3
// 1
Implementation
Matrix sort({List<int>? columnIndices, bool ascending = true}) {
if (toList().isEmpty || this[0].isEmpty) {
throw Exception('Matrix is empty');
}
Matrix sortedMatrix = Matrix([
for (int i = 0; i < rowCount; i++)
[for (int j = 0; j < columnCount; j++) this[i][j]]
]);
// Sort all elements in ascending or descending order
if (columnIndices == null || columnIndices.isEmpty) {
List<dynamic> elements =
sortedMatrix.toList().expand((row) => row).toList();
elements.sort((a, b) => ascending ? a.compareTo(b) : b.compareTo(a));
int k = 0;
for (int i = 0; i < rowCount; i++) {
for (int j = 0; j < columnCount; j++) {
sortedMatrix[i][j] = elements[k++];
}
}
} else {
// Validate column indices
for (int columnIndex in columnIndices) {
if (columnIndex < 0 || columnIndex >= columnCount) {
throw Exception('Invalid column index for sorting');
}
}
sortedMatrix._data.sort((a, b) {
for (int columnIndex in columnIndices) {
int comparison = a[columnIndex].compareTo(b[columnIndex]);
if (comparison != 0) {
return ascending ? comparison : -comparison;
}
}
return 0;
});
}
return sortedMatrix;
}