JavaScript is used for sorting an array by transposing the elements

When I transpose my array 'solverPacks_', it creates columns for Excel. However, the values in the columns are not sorted from high to low...

var transposeToExcel = solverPacks_[0]
    .map((_, colIndex) => solverPacks_.map(row => row[colIndex])
      .join('\t')
    ).join('\n');
  console.log("transposeToExcel = " + "\n" + transposeToExcel);

Is there a way to sort the numbers from high to low in the columns while transposing? I would like it to look like this (showing only the first column):

4298 4275 4228 4199 4189 4012 4008 3700 3659 3614 3595 3579

Thank you for taking the time to read this! Christina

Answer №1

If you're looking to organize your data by column, you'll need to rearrange those columns first. For example, converting an array like [[1,2],[3,4]] into [[1,3],[2,4]]. Once you've done that, you can proceed with sorting the data and then revert it back to its original form:

const dataSet = [
  [8,3,5],
  [9,2,6],
  [7,4,1]
]

const transposeData = (rows) => Array.from({ length: rows.length }, (_, index) => rows.map(row => row[index]))
const sortedData = transposeData(transposeData(dataSet).map(arr => arr.sort().reverse()))
console.log(sortedData.map(JSON.stringify))

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The Angular application must remain logged in until it is closed in the browser

Currently, my Angular app includes authentication functionality that is working smoothly. The only issue is that the application/session/token expires when there is no user activity and the app remains open in the browser. I am looking for a solution wher ...

How to verify if an object is empty in an AngularJS expression

I need to display either a Login or Logout button based on the value of a $rootScope variable. Currently, only the Logout button is showing up in the li tag below. I have specific actions that should occur after certain events: After Logging In:- $root ...

Tips for aligning an element in the center in relation to its sibling above

Help Needed with Centering Form Button https://i.sstatic.net/QhWqi.png I am struggling to center the "Send" button below the textarea in the form shown in the image. Despite trying various methods like using position absolute, relative, and custom margin ...

Retrieve the calculated events of an HTML element using JavaScript

Is there a way to retrieve the computed click events from an HTML object using JavaScript? I want to access the functions that are triggered when clicking on an HTML element. Is there a method to directly obtain the function calls instead of using the s ...

What could be the reason behind three.js SVGLoader flipping SVGs upside down during rendering?

My solution for rendering svgs in three.js involves using the group.scale.y = -1; trick, but it has created an issue with the y coordinate system. When I apply this trick to correct the upside-down svg rendering, the positive y coordinates no longer push t ...

Applying a CSS class to a newly generated row with JavaScript

I have a JSP page where I dynamically add rows to a table using a different Javascript function than in my previous query. While I can add elements to the table columns, I'm unable to apply a style class that is defined in a CSS file. Here is my Java ...

Can you explain the concept behind the event processing loop in Node.js?

Currently, I am reviewing a gist that outlines a file walk algorithm in JavaScript // ES6 version using asynchronous iterators, compatible with node v10.0+ const fs = require("fs"); const path = require("path"); async function* walk(d ...

execute a jQuery function within a data list component

Looking to slide down a div and then slide it back up when a button is clicked. After searching on Google, I found a solution but unfortunately, the slide up function is not working as expected. When the button is clicked, the div slides down correctly but ...

What is the best method for managing file storage and retrieval in web applications that only run on the client-side?

I'm currently working on an application where users can create their own data. Without a backend or database, all the information is stored within the user's session. My preferred solution involves: User interacts with the app, clicks "Save", ...

Exploring the hidden contents within the zip file

I have been exploring methods for reading 'zip' files. Two libraries that I have used are zip.js and JSzip. I have successfully viewed the contents of the zip file. However, here lies the challenge: My goal is to identify specific file types ...

Locate the hyperlink within a div using JavaScript and navigate to it

I stumbled upon an element (div1) and now I am looking for a link within that element (link) so that I can navigate to it. <div class="div1"> <p> <a href="link">Link</a> </p> </div> ...

I am looking for a highly specialized jQuery selector that fits my exact requirements

Hello everyone, I'm encountering an issue with a jQuery selector. Here is the HTML code snippet: <div id="Id_Province_chzn"> <a href="javascript:void(0)" class="chzn-single chzn-default" tabindex="-1"> <span> + this.default_tex ...

Tips for detecting when no checkboxes in a group are selected or when at least one checkbox is selected, and then applying a class to the corresponding div

<div class="accordion-group"> <div class="accordion-heading"> <a href="#collapse" data-parent="#accordionQuiz" data-toggle="collapse1.." class="accordion-toggle"> <strong>1...</strong> Question ...

Leveraging icons with Bootstrap 4.5

I am currently exploring how to incorporate Bootstrap 4.5 icons using CSS. Do you have any examples of code that could guide me on how to achieve this? I am specifically interested in understanding the required CSS declarations that would allow me to use t ...

Divide the data received from an AJAX request

After making my ajax request, I am facing an issue where two values are being returned as one when I retrieve them using "data". Javascript $(document).ready(function() { $.ajax({ type: 'POST', url: 'checkinfo.php', data: ...

Identify data points on the line chart that fall outside the specified range with ng2-charts

I'm struggling to figure out how to highlight specific points on a line chart that fall outside a certain range. For instance, if the blood sugar level is below 120, I want to display that point as an orange dot. If it's above 180, I want to show ...

Processing requests through Axios and Express using the methods GET, POST, PUT, and DELETE

When working with express router and Axios (as well as many other frameworks/APIs), the use of GET/POST/PUT/DELETE methods is common. Why are these methods specified, and what are their differences? I understand that a GET request is used to retrieve dat ...

Tips for ensuring sequential execution of $.post requests without ajax alternative

Can I make synchronous requests with $.post in this code snippet? function loadTest() { var questionIDs = []; var count = 0; console.log("getting test"); $.post("db.php", function(data) { obj = jQuery.parseJSON(data); var questionCount = obj.l ...

What is the process for obtaining X through the use of createElement('img')?

Is there a way to retrieve the X position from the left of the tag created using document.createElement('img'); var block00 = document.createElement("img"); block00.src = "images/sep1.png"; If so, how can it be done: if (block00.getBoundingCli ...

Removing elements from an array with reduced speed

Within my array, I am looking to remove N elements from the beginning. For instance, if my array contains 1 million floating point elements and I need to remove the first 500,000, I have two options. The first is to iterate and call the shift method 500,0 ...