Sorting function encounters issues in Internet Explorer 7

I've been analyzing this JavaScript code for quite some time now and I'm stuck.

It's a case-insensitive sort function that handles special (Danish) characters properly, but for some reason, it doesn't work in IE7. I can't figure out why because it doesn't seem too complex...

function _compare(a, b) {
    var sortorder = " 0123456789.abcdefghijklmnopqrstuvwxyzæøå",
        min = 0,
        idx = 0,
        c = 0;
    a = ' ' + a;
    b = ' ' + b;
    a = a.substring(1).toLowerCase();
    b = b.substring(1).toLowerCase();

    min = Math.min(a.length, b.length);

    while (idx < min && a[idx] == b[idx]) {
        idx++;
    }

    if (idx == min) {
        if (a.length > b.length) {
            c = 1;
        }
        else if (a.length < b.length) {
            c = -1;
        }
    }
    else {
        c = (sortorder.indexOf(a[idx]) > sortorder.indexOf(b[idx])) ? 1 : -1;
    }

    return c;
}

var key, ar = [];

ar.push("TEST");
ar.push("abcd");
ar.push("test");
ar.push("øæå!");
ar.push("oåø!");
ar.push("åæø!");
ar.push("aaø!");

ar.sort(_compare);

for (key in ar) {
    $("pre").append(ar[key] + "<br />");
}

http://jsfiddle.net/hpvek/ - it works as expected in FF, Chrome, IE8-9 and Safari. IE7 (possibly IE6, which I don't have access to for testing) seems to be the outlier here.

Thank you for any assistance.

Answer №1

for (let item of array) {
    $("pre").append(array[item] + "<br />");
}

If you want to maintain the order, this is not the correct way to iterate through an array.

However, it doesn't seem to be causing any issues in this case.

Oh, I see now. Internet Explorer does not support treating strings as arrays. You must convert them into arrays to access specific indices or use charAt.

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

Parsing JSON multiple times to extract data on the top 10 games from Twitch's API

Hey there! I have a little coding challenge for you. The current code below is only fetching the first channel from the JSON. My goal is to fetch all the streams and display them in a grid format with three streams per row. Can you help me achieve this? (B ...

The JQuery.ajax function encountered an unexpected identifier and threw an Uncaught SyntaxError

Hey there, I'm encountering this error and I'm stumped: jQuery.ajax Uncaught SyntaxError: Unexpected identifier I'm attempting to pass some customer information to another file named "pricealarm.php" in the main directory of the FTP-Serve ...

Google Maps API is successfully loading from an HTML file, however, it is not functioning properly when accessed

I am facing an issue while trying to connect to the Google Maps API through an AngularJS application on my localhost. Despite finding the javascript file in the HTML and displaying 'test1' in the console, the `initMap` function is not being calle ...

Linking functions as needed

I apologize for the not-so-great title of this question. Basically, I have a library that generates users with predetermined capabilities. Currently, it involves creating a new user instance and calling asynchronous methods like: var User = require(...).U ...

What is the best way to pass a URL in Vue.js methods and activate it on @click event?

Currently, I am working on a Laravel Vue.js project where I am trying to implement a method to redirect to a link upon clicking a div element. The code snippet provided below showcases my approach. When the user clicks on the div, I want them to be direc ...

The importance of utilizing an object FormData in Node.js to ensure the proper functioning of multer for file uploads via ajax with jQuery

Currently, I am learning about Node.js and using Multer, AJAX, and jQuery for file uploads. However, I am feeling a bit confused. Let's say I have the following code in an HTML file: <form action="/" method='post' enctype=" ...

Storing div content in database directly

I have a straightforward div that allows users to edit content on the page. I need to save this content in a database, but without including any HTML tags while still preserving line breaks. Currently, I am using the innerText property for this purpose. N ...

Here's how you can arrange a list starting with the first item and then searching for a specific string using md-autocomplete in

As a newcomer to angularJs, I am looking for ways to filter search results more efficiently. Check out this example here: https://codepen.io/anon/pen/mpJyKm I am trying to customize the search result by filtering based on query input. Specifically, I wan ...

Tips for preventing redirection to the login page upon page refresh in a next.js application

I've been diving into user authentication and authorization using next.js. I managed to create a custom authguard hook that works well, except for one issue - when a user logs in and refreshes the page, they are redirected back to the login page. Aut ...

Displaying a variable in a live HTML user interface

I have successfully created a Python program that captures data from an Arduino Potentiometer and shows it on the Python console. Now, I am working on enhancing the output by displaying it in a local HTML file. I am seeking guidance on how to incorporate t ...

What is the best way to connect a relative CSS stylesheet to a master page?

My Java application generates several HTML pages, organized in different directories: html_pages/ | ----> landin_page.html html_pages/details | ----> index1.html html_pages/more_details | ----> index2.html html_pages/css | ...

Filter products by price using Ajax in Shopify with Liquid code

I am looking to implement a custom product price filtering feature for my collection without relying on an app. Unlike the traditional price filter option, I want users to be able to input any combination of numbers, such as between $4 and $20 or $7 and $3 ...

Alter the default time zone in JavaScript

PHP features a function known as date_default_timezone_set which impacts the GMT time that is used by the Date() command. Is there a way for this function to also impact JavaScript? Here is a custom function I have: function calcTime(offset) { d = n ...

Concealing choices that have already been chosen in a ReactJS select dropdown menu

My challenge involves having 3 select boxes with the same option values, where users set security questions. Each question is selected and answered by the user. I fetched security questions via an API call and stored them in an array named "securityQuestio ...

Ensure that the chart.js doughnut remains centered in its container with a fixed width and height

I'm currently utilizing the aurelia plugin for chart js to create a dynamic Doughnut chart. I have disabled the responsive feature for the chart so that it maintains a fixed width and height regardless of screen or device size. However, I am facing an ...

Utilize Array in Form Input with Index and Spread Operator

Looking to create a form that can handle array data with dynamic fields in TypeScript. Encountering the following error: Element implicitly has an 'any' type because expression of type 'any' can't be used to index type '{ nam ...

Check if a certain object is devoid of any values based on the elements in an array - JavaScript

I have collected data related to various businesses: { "business": { "type": [ "LLC", "Corporation" ], "LLC": { "status": "acti ...

Retrieve an image from the database and associate it with corresponding features or news in PHP

I have retrieved a value from the database, displayed it as an image, and made it a link. So, I want that when a user clicks on the different image, they get the result from the query related to the image. I hope everyone understands. <?php // Connect ...

What is the method to determine the size of a Map object in Firestore database?

I currently have two elements within a document: an empty array, and a map object containing three components. If the array is empty, it transforms into type array. In this case, I can execute console.log(vehicles.Motorcycles.length) to receive a return of ...

executing a hook within _app.tsx in Next.js

The issue I'm facing involves the rendering of a hook that helps determine if my components are within the screen's view to trigger animations. This hook is executed in _app.tsx, however, it doesn't run when switching to another page. Oddly ...