Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function ascendingSort(array) {
- return array.sort(function (a, b) {
- return b - a;
- }).reverse();
- }
- function descendingSort(array) {
- return array.sort(function (a, b) {
- return b - a;
- });
- }
- // ascending sort for even rows
- // descending sort for odd rows
- function sortRowsInMatrix(matrix) {
- var currentRow = [];
- for (var i = 0; i < matrix.length; i++) {
- for (var j = 0; j < matrix.length; j++) {
- currentRow.push(matrix[i][j]);
- }
- if (i % 2 === 0) {
- currentRow = ascendingSort(currentRow);
- }
- else {
- currentRow = descendingSort(currentRow);
- }
- for (var j = 0; j < matrix.length; j++) {
- matrix[i][j] = currentRow[j];
- }
- currentRow = [];
- }
- return matrix;
- }
- var matrix = [[3, 2, 1], [4, 5, 6], [9, 8, 7]];
- console.log(sortRowsInMatrix(matrix));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement