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 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

Related Questions

I have a javascript function that has been driving me nuts. This is the
I have a javascript function that calls another function, I am now facing a
I have a PHP function that returns an array. This is the output of
I have a simple javascript function that takes two variables. I need to pass
I have a javascript function that reads an entire form into a serialized $.ajax({
I have a javascript function: string.prototype.startsWith = function(str) { return (this.indexOf(str) === 0); }
I have encountered a very strange bug in Firefox. I have a javascript function
I have these check boxes that are created with a javascript function: I would
In one HTML page I have a JavaScript function checkUpdates() that hits the server.
I have a javascript function that I am calling from an image onClick event

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.