[javacript] IE won't allow object manipulation within sort method

Magentix

if (OP.statement == false) postCount++;
Reaction score
107
Hey guys, I've encountered a rather annoying problem with IE7 (and 6/8?)

I'm trying to sort an array of objects, but after swapping their places in the array, I want to let the objects have some parameters of their "old place".

In the example below, I want to have 3 names called out: A, B and C. Now I want A, B and C to have ascending ages, without calling them in a different order.

Solution: I grab my magic stick and give A the youngest age, B the middle age and C the oldest one.

The code below works fine in FF/Chrome, but IE won't let me manipulate objects that are passed to the .sort() method.


To get you started, the process of the first loop goes as follows:

A-3,B-1,C-2
-> detect lower age, swap names ->
B-3,A-1,C-2
-> because the func returns +2, javascript .sort() swaps the objects ->
A-1,B-3,C-2
-> because a swap occured (even though only age swapped), .sort() continues normally

(To test the case below you'll need jQuery)

Code:
function test() {
	var obj = [{'name':'A','age':3},{'name':'B','age':1},{'name':'C','age':2}];
	
	$.each(obj,function(){
		alert(this.name+', aged: '+this.age);
	});
	
	obj.sort(function(a,b){
		if (a.age > b.age) {
			var tempobj = b.name;
			b.name = a.name;
			a.name = tempobj;
		}
		return a.age - b.age;
	});
	
	$.each(obj,function(){
		alert(this.name+', aged: '+this.age);
	});
}
 

JerseyFoo

1/g = g-1
Reaction score
40
Try...

Code:
function walk(a,b){
		if (a.age > b.age) {
			var tempobj = b.name;
			b.name = a.name;
			a.name = tempobj;
		}
		return a.age - b.age;
}

function test() {
	var obj = [{'name':'A','age':3},{'name':'B','age':1},{'name':'C','age':2}];
	
	$.each(obj,function(){
		alert(this.name+', aged: '+this.age);
	});
	
	obj.sort(walk);
	
	$.each(obj,function(){
		alert(this.name+', aged: '+this.age);
	});
}

Really shouldn't be using an anonymous function for that anyway. (or using jQuery)

Code:
	$.each(obj,function(){
		alert(this.name+', aged: '+this.age);
	});
..could be...
Code:
var a, i;
for ( i = 0; (a = obj[i]); i++ ){
    alert(a.name+', aged: '+a.age);
}
 

Magentix

if (OP.statement == false) postCount++;
Reaction score
107
Nope, doesn't change anything.

Fact that it needs jQuery is because it's a snippet from my code (with names changed for comprehension purposes)

I think it's the way IE sorts, since putting an alert in the sort function shows a different 'path' in IE than in Chrome/FF.

It also thinks 3 < 1... go figure


EDIT:
It's all about not being able to manipulate passed objects...

Run this in FF and then IE
Code:
function test() {
	var obj = [{'name':'A','age':3},{'name':'B','age':1},{'name':'C','age':2}];
	
	$.each(obj,function(){
		alert(this.name+', aged: '+this.age);
	});
	
	obj.sort(function(a,b){
		if (a.name == 'A') { a.name = 'AAA'; }
		return a.age - b.age;
	});
	
	$.each(obj,function(){
		alert(this.name+', aged: '+this.age);
	});
}

FF output: A, aged: 3 -> B, aged: 1 -> C, aged: 2 -> B, aged: 1 -> C, aged: 2 -> AAA, aged: 3
IE output: A, aged: 3 -> B, aged: 1 -> C, aged: 2 -> B, aged: 1 -> C, aged: 2 -> A, aged: 3
 

Magentix

if (OP.statement == false) postCount++;
Reaction score
107
Solved the issue by writing my own object array sorting function.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top