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 6918815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:54:29+00:00 2026-05-27T09:54:29+00:00

I have a function to sort a JSON object that looks like this: function

  • 0

I have a function to sort a JSON object that looks like this:

function compare(el1, el2, index) {
      return el1[index] == el2[index] ? 0 : (el1[index] < el2[index] ?-1 : 1);
   }
   array.sort(function(el1,el2){
      return compare(el1, el2, ""+thSortKey+"");
   });

The entire thing can be found here. This function is run when a table’s <th> is clicked on. The way it’s setup now allows for A-Z sorting, but on a second/alternate click I want to sort it from Z-A. The problem I’m running into is keeping track of the clicks. I tried using .data() to no avail, and .toggle() wouldn’t work for me in this situation. My question is how do I keep track of clicks so that I can allow for Z-A sorting?

  • 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-05-27T09:54:30+00:00Added an answer on May 27, 2026 at 9:54 am

    Try introducing a dirCols object with key = headerName and value = direction.

    Then:

    var dirCols = {};                          // <- added ... directions for columns
    $('table tr th').live("click", function() {
      table = $('<table border="1"><tr>'+headersString+'</tr>');
      var thSortKey = $(this).text();
      var dir = dirCols[$(this).text()] || 1;  // <- added ... get previous dir or 1
      dirCols[$(this).text()] = -1 * dir;      // <- added ... save inverted dir
      function compare(el1, el2, index) {      // <- modified ... use dir in compare
        return el1[index] == el2[index] ? 0 : dir * (el1[index] < el2[index] ?-1 : 1);
      }
      // rest of the code
    

    Here is the code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JSON object that I want to sort by one key first,
with reference of this question Sort json array I have the following JSON String
I have a JSON array like this [{Email:someone@some.com,Name:ACO,Groups:[MOD_SW,MOD_PI,MOD_GE],Id:63,Url:aco}, {Email:someone@some.com,Name:Agpo,Groups:[MOD_PI],Id:22,Url:agpo}, {Email:someone@some.com,Name:Akatherm,Groups:[MOD_SW],Id:64,Url:akatherm}, {Email:someone@some.com,Name:Albrand,Groups:[MOD_PI,MOD_GE],Id:23,Url:albrand}] I want to
I have this function that runs over returned json data. It is really fast
In .NET, the Generics Lists have a sort function that accepts IComparer or Comparison
I have a JSon array that displays in a JQuery Mobile list. It shows
Right now I have something like this.... Item.find({}, function (docs) { for (var i
I have some sort of recursive function, but I need to parse a string,
i have function public Menu Details(int? id) { return _dataContext.Menu.Include(ChildMenu).FirstOrDefault(m => m.MenuId == id);
I have function getCartItems in cart.js and I want to call that function in

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.