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

  • Home
  • SEARCH
  • 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 833573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:32:23+00:00 2026-05-15T04:32:23+00:00

I have a line in a javascript function that sort an array of objects

  • 0

I have a line in a javascript function that sort an array of objects based on the order of another array of strings. This is working in firefox but not in IE and i don’t know why. Here’s what my data looks like going into the sort call, in IE7. (I’m using an array of three items just to illustrate the point here).

//cherry first then the rest in alphabetical order
originalData = ['cherry','apple','banana','clementine','nectarine','plum']

//data before sorting - note how clementine is second item - we wan to to to be after apple and banana
csub = [
  {"value":"cherry","data":["cherry"],"result":"cherry"},
  {"value":"clementine","data":["clementine"],"result":"clementine"},
  {"value":"apple","data":["apple"],"result":"apple"},
  {"value":"banana","data":["banana"],"result":"banana"},
  {"value":"nectarine","data":["nectarine"],"result":"nectarine"},
  {"value":"plum","data":["plum"],"result":"plum"}
]

//after sorting, csub has been rearranged but still isn't right: clementine is before banana. in FF it's in the right place.
csubSorted = [
  {"value":"cherry","data":["cherry"],"result":"cherry"},
  {"value":"apple","data":["apple"],"result":"apple"},
  {"value":"clementine","data":["clementine"],"result":"clementine"},
  {"value":"banana","data":["banana"],"result":"banana"},
  {"value":"nectarine","data":["nectarine"],"result":"nectarine"},
  {"value":"plum","data":["plum"],"result":"plum"}
]

Here’s the actual sort code:

 csubSorted = csub.sort(function(a,b){ return (originalData.indexOf(a.value) > originalData.indexOf(b.value)); });

Can anyone see why this wouldn’t work? Is the basic javascript sort function not cross-browser compatible? Can i do this a different way (eg with jquery) that would be cross-browser?

grateful for any advice – max

EDIT – this also fails to work in safari and chrome – in other words, it only seems to work in firefox.

SOLVED – thanks to Tim Down.
I’d actually made my code simpler because i realised that the order that i needed was always “the first item in the returned array followed by the rest of the array sorted using .value”. So, i changed my code thus:

  first = csub.shift();
  csubSorted = csub.sort(function(a,b){ 
    return (a.value > b.value); 
  });
  csubSorted.unshift(first);

But, it still wasn’t working. Then Tim (below) pointed out that sort expects to get a -1, 0 or 1 back from the function, NOT true or false which is what my code was returning. Obviously firefox lets you get away with this, but the other browsers don’t. All that was required was to ‘translate’ true or false into 1 and -1 (i don’t worry about the case where both strings are the samae, effectively that will get returned as -1 which wouldn’t make any difference to the sort order anyway):

  first = csub.shift();
  csubSorted = csub.sort(function(a,b){ 
    return (a.value > b.value ? 1 : -1); 
  });
  csubSorted.unshift(first);

Tim also told me that array.indexOf() isn’t supported in IE which is annoying as even though i’m not using it here any more i am using it in other bits of code. Goddamit. Is there an API page somewhere which definitively lists only the cross-browser compatible javscript API?

  • 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-15T04:32:23+00:00Added an answer on May 15, 2026 at 4:32 am

    First, there’s no indexOfmethod of Array in IE <= 8. You’ll need to write your own. Second, the comparison function passed to the sort() method of an Array should return a number rather than a Boolean.

    var indexOf = (typeof Array.prototype.indexOf == "function") ?
        function(arr, val) {
            return arr.indexOf(val);
        } :
    
        function(arr, val) {
            for (var i = 0, len = arr.length; i < len; ++i) {
                if (typeof arr[i] != "undefined" && arr[i] === val) {
                    return i;
                }
            }
            return -1;
        };
    
    csubSorted = csub.sort(function(a,b){
        return indexOf(originalData, a.value) - indexOf(originalData, b.value);
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 542k
  • Answers 542k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I added custom html tags to the elements i needed… May 17, 2026 at 3:13 am
  • Editorial Team
    Editorial Team added an answer The arguments for such parameters are passed in the usual… May 17, 2026 at 3:13 am
  • Editorial Team
    Editorial Team added an answer AFAIK, it is typically stored in the ReportServer database in… May 17, 2026 at 3:13 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have a PHP function that returns an array. This is the output of
I have this javascript function I am trying to understand. I don't know if
I have a bit of JavaScript code that is specified in a configuration file
I have this piece of javascript which is supposed to validate on the clientside.
I have added a simple .js file to my page that has some pretty
I've got a function that runs a user generated Regex. However, if the user
I am working on a classic asp form that has a number of dropdowns.
I have the following that will enumerate over the model and add an invoice
I have a DOM element similar to Facebook's View friends popup. I have a
I am trying to debug a large and complex webapp that makes heavy use

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.