Suggestions for customizing this comparison function for sorting tables

When sorting alphabetically, I want to prioritize anything that begins with [ or . before the rest. How can this be achieved?

function ts_sort_default(a,b) {
  aa = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]);
  bb = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]);
  if (aa==bb) {
    return 0;
  }
  if (aa<bb) {
    return -1;
  }
  return 1;
}

Answer №1

To incorporate this functionality, simply include the following code snippet:

function custom_sort_method(a,b) {
  aa = getInnerText(a.cells[INDEX_TO_SORT]);
  bb = getInnerText(b.cells[INDEX_TO_SORT]);

////
if(aa.substr(0,1) == '[') {
        if(bb.substr(0,1) == '[') {return 0;} else {return 1;}    
}

if(bb.substr(0,1) == '[') {
    return -1; // aa does not start with "["    
}

if(aa.substr(0,1) == '.') {
    if(bb.substr(0,1) == '.') {return 0;} else {return 1;}    
}

if(bb.substr(0,1) == '.') {
    return -1;    
}

////

if (aa==bb) {
    return 0;
  }
  if (aa<bb) {
    return -1;
  }
  return 1;
}

(Please verify the JavaScript syntax as I am not overly familiar with it.)

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

I have encountered an issue where after declaring a JavaScript variable on a specific page, including the JavaScript file does not grant access to it

<script type="text/javascript"> $(document).ready(function () { var SOME_ID= 234; }); </script> <script type="text/javascript" src="<%= HtmlExtension.ScriptFile("~/somefile.js") %>"></script> ...

Retrieve the name of the file from a given URL by using JavaScript/jQuery, even when only the directory path is

I need to extract the current filename from the URL using: $currentFile = window.location.pathname.split("/").pop(); This method functions properly when the full path looks like: http://www.yoursite.com/folder/index.php It will return index.php, index. ...

I'm currently working with ReactJS and attempting to retrieve JSON data from a REST API in JIRA, but I'm facing challenges in achieving this

I've been struggling for hours trying to understand why I am unable to access and transfer data in my array from the JSON data in JIRA using the REST API. Basically, I am attempting to retrieve the JSON data from the JIRA website via URL with Basic Au ...

Encountered the error "Unable to update state on unmounted component in React because of timeout"

Despite the abundance of information available online about this particular warning, I am still struggling to find a solution that applies to my specific scenario. Currently, I am developing an autosave feature for a form component where typing triggers a ...

Passing asynchronous data to child components using an object in Angular6

Currently, I am facing an issue with displaying data retrieved from a server using Angular 6, Rxjs, and Chartjs. When utilizing local mock data, everything works perfectly fine. However, when fetching data from the server, the charts render as blank becaus ...

Choose an option from the selection menu automatically using a link

I am looking to utilize either PHP or JavaScript in order to link to a page and add either "?type=foo" or "#foo" at the end of the URL. This will allow for a specific option to be automatically selected from a dropdown menu within a form when the linked ...

Express server unable to process Fetch POST request body

I'm currently developing a React app and I've configured a basic Express API to store user details in the database app.post("/register", jsonParser, (req, res) => { console.log("body is ", req.body); let { usern ...

Uncovering the value within a forEach loop in Node.js

I'm currently working on a basic application where, by passing a command line parameter, the app will search through a directory. If the text matches in any of the files, that file should be added to a list. However, I've noticed that when I use ...

assign a random name to the "attribute" of any object

I recently started using TypeScript and I have a question about the syntax. I came across some code that defines a parameter like this: { [property: string]: any} I'm a bit confused because I understand that the parameter should be an object and its ...

Display a specific element only if another element exceeds a specified height

A snippet of HTML code is given below: <span class="day-number">{{day-number}}</span> <div class="event-box"> <div class="event-container"> </div> <div class="more-events">more ...</div> </div> The .e ...

Video embedded from YouTube refuses to loop playback

I am having trouble getting a YouTube video to play in a loop. <iframe id="music" width="1" height="1" src="https://www.youtube.com/embed/jAsSgK3CvO0?playlist=jAsSgK3CvO0&autoplay=1&loop=1"> </iframe> After the video loops ...

The error message "AWS Lambda Node 18: fetch is not defined, please resolve or include global fetch" indicates that

Seeking assistance with calling the Pagespeed Insights API from an AWS Lambda using Node 18. Struggling to make fetch() function properly. Cloudwatch logs indicate the message "inside the try about to fetch," but then nothing else appears. The Lambda con ...

Using jQuery to extract a specific portion of a URL path

Seeking assistance with extracting category information from lengthy URLs and assigning it to a variable. Consider the following URL: http://example.com/community/home/whatever.html The goal is to assign the variable to the folder path that follows /hom ...

What is the best way to prevent event propagation in d3 with TypeScript?

When working with JavaScript, I often use the following code to prevent event propagation when dragging something. var drag = d3.behavior.drag() .origin(function(d) { return d; }) .on('dragstart', function(e) { d3.event.sourceEvent ...

How to style date strings in JavaScript, jQuery, or CSS

Below are dates that need formatting: 7-sep, 14-sep, 21-sep, 28-sep, 5-oct,12-oct, 19-oct,26-oct, 2-nov, 9-nov, 16-nov, 23-nov,30-nov, 7-dec,14-dec, 21-dec,28-dec-2013. Is there a way to tidy them up using JavaScript, jQuery, or CSS? Ideally, I would lik ...

Formulate a multi-line string using a collection in React's JavaScript framework

I'm working on a React function that involves a set and I need to update an HTML element using the data from this set. Below is an example of my code: const updateElement = (mySet) => { document.getElementById('myId').innerHTML = Arra ...

Guide to dynamically generating MongoDB schemas with Node.js in a flexible and adaptable manner

Is it feasible to dynamically generate a table in mongodb using Mongoose schema with Node.js and Angular? Instead of explicitly creating a model in Node.js, you can use the following example: import mongoose from 'mongoose'; const Schema = mong ...

Retrieve Gridview properties using JavaScript

I need to adjust the font size of my gridview using JavaScript to make it more suitable for printing. What is the best way to change the font size specifically for a gridview using JavaScript? ...

Issue with 'for' loop iteration over object array

Can someone help me understand why I am getting the unexpected result "6 You have not watched undefined undefined" in the browser console when running this simple code? var films = [{ title: "The Mummy", hasWatched: true, stars: "5 stars" ...

Resolve the issue of autofill data overlapping with field labels

My form incorporates the Canada Post AddressComplete tool, which functions similarly to Google Maps Autocomplete. As you type your address, suggestions appear in a drop-down menu. However, after selecting an address, the text in the Postal Code and City f ...