Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8715461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:51:02+00:00 2026-06-13T05:51:02+00:00

I am trying to use the sort function to sort rows in a table

  • 0

I am trying to use the sort function to sort rows in a table in ascending order.

var forsort = d3.keys(csv[0]).filter(function(key) {
    return key != "name";
});

var tr = d3.selectAll("tbody tr");      
d3.selectAll("thead th").data(forsort).on("click", function(k) {
    tr.sort(d3.ascending);
});

For some reason only 3 rows out of 18 are being swapped (rows 2,3,11 to be precise) with row 2 being the first sortable row.

Can someone tell me what the problem can be?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T05:51:03+00:00Added an answer on June 13, 2026 at 5:51 am

    From the d3 documentation, d3.ascending looks like this

    function(a, b) {
      return a < b ? -1 : a > b ? 1 : 0;
    }
    

    What are a and b in your case? You can check by, instead, sorting like this:

    tr.sort(function(a,b){ console.log('compare', a, b); });

    I’m guessing that a and b are objects, in which case sorting them with greater/less -than would be meaningless.

    Edit:

    To sort by the key of the column that was clicked, you need to use the k attribute you’re getting from the click handler (I have no way of verifying this, but I’m pretty sure that’s what k is – a string corresponding to the column/attribute name).

    So, ditch the use of d3.ascending and do the sorting like this:

    tr.sort(function(a, b) {
      return a[k] < b[k] ? -1 : a[k] > b[k] ? 1 : 0;
    })
    

    Alternatively, you can still use d3.ascending like this:

    tr.sort(function(a, b) {
      return d3.ascending(a[k], b[k]);
    })
    

    It’s not much of a difference in principle. But, one benefit of the latter is that it makes it easier to implement something that’ll give users control over sort direction (ascending vs descending). Something like this:

    var sortFunction = d3.ascending;
    // Instead of the line above, you could write some logic
    // to decide whether sortFunction is d3.ascending or d3.descending
    
    // Then you can write:
    tr.sort(function(a, b) {
      return sortFunction(a[k], b[k]);
    })
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use stl sort() in a class function. I would like to
I am trying to use the .sort() function to sort a list so: <div
I am trying to use a sorting function for grade, which will sort name
I'm trying to use boost::bind with the std::sort function. I want to bind sort
I'm trying to use the bubble sort method to sort an array of only
I am trying to use MPI to sort digits, after sorting by the different
I'm trying to use Twisted in a sort of spidering program that manages multiple
I am trying to use jQuery's draggable and sortable to sort elements listed inside
I was trying use a set of filter functions to run the appropriate routine,
I'm trying to use TF-IDF to sort documents into categories. I've calculated the tf_idf

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.