Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Sorting an Arrayobject through javascript

1. ## Sorting an Arrayobject through javascript

Hi Guys ,

I have Array of objects , where we have combinations of integers and strings and now I want sort these objects ,

Example of Array [ 1333, A12, b233, Abc , zaa]

the output should as Abc,A12,b233,zaa,1333

I need the code snippet fot this

• First - you're not sorting an array of objects
If you did I would recommend
http://www.thomasfrank.se/sorting_things.html

It's a bit unclear what rules you would apply for sorting. If you wanted all the numbers sorted numerically and placed after all the strings then this would work:

Code:
```var a = [1333, 'A12', 'b233', 'Abc' , 'zaa'];
a.sort(function(x,y){
return x > y ? 1 : -1
});
However that would return
A12 Abc b233 zaa 1333
which is not the order you seem to want...

• Hmm. The ASCII values for numerals come before those for letters, so I do not see how it is possible to sort in alphabetical order but with numbers ranking after letters.

This partially fulfills your need:-

Code:
```<script type = "text/javascript">

var myArray = new Array('1333','A12','b223','Abc','zaa', '2345');
function x() {
for (var i = 0; i <myArray.length; i++) {
if (/^[0-9]/.test(myArray[i])) {
myArray[i]= "zzzzzzzzzz" + myArray[i] ;  // temporary prefix
}
}

myArray.sort(function(a,b) {   // perform the sort
var x = String(a).toLowerCase();
var y = String(b).toLowerCase();
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
});

for (var i = 0; i <myArray.length; i++) {
myArray[i] = myArray[i].replace(/zzzzzzzzzz/,"");  // remove temporary prefix
}
document.write ("Custom sorted array: " + myArray);   // A12,Abc,b223,zaa,1333,2345
}

x();
</script>```
All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

• // This won't work in old browsers without providing a map method,
// but the requirements for sorting are too specific to bother doing it the long way.

var A1=[1333, 'A12', 'b233', 'Abc', 'zaa'];
Code:
```A1.sort(function(a, b){
if(typeof a== 'number' || typeof b== 'number'){
if(typeof a!= 'number') return -1;
if(typeof b!= 'number') return 1;
return a-b;
}
var i= 0,
f= function(c){
c= c.charCodeAt(0);
if(c<58 && c> 47) c+= 1000;
return c;
},
a1= a.toLowerCase().split('').map(f),
b1= b.toLowerCase().split('').map(f),
L= a1.length;
while(i<L){
if(!b1[i] || a1[i]> b1[i]) return 1;
if(a1[i]<b1[i]) return -1;
++i;
}
return b1[i]? -1: 0;
});```
/* returned value: (Array)
Abc,A12,b233,zaa,1333
*/
Code:
```Array.prototype.map=[].map ||
function map(fun, scope){
var T= this, L= T.length, A= Array(L), i= 0;
if(typeof fun== 'function'){
while(i< L){
if(i in T){
A[i]= fun.call(scope, T[i], i, T);
}
++i;
}
return A;
}
}```

• Solved "the long way" which works in IE6. But your required sort order is weird.

Code:
```<script type = "text/javascript">

var myArray = new Array('1333','A12','b223','Abc','zaa', '2345', 'A13', 'BBC');

function x() {

for (var i =0; i <myArray.length; i++) {
myArray[i] = myArray[i].replace(/^(\D+)(\d)/gi, "\$1zzzzzzzzzz\$2");
}

for (var i = 0; i <myArray.length; i++) {
if (/^[0-9]/.test(myArray[i])) {
myArray[i]= "zzzzzzzzzz" + myArray[i] ;  // temporary prefix
}
}

myArray.sort(function(a,b) {   // perform the sort
var x = String(a).toLowerCase();
var y = String(b).toLowerCase();
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
});

for (var i = 0; i <myArray.length; i++) {
myArray[i] = myArray[i].replace(/zzzzzzzzzz/,"");  // remove temporary prefix
}
document.write ("Custom sorted array: " + myArray);   // Abc,A12,A13,BBC,b223,zaa,1333,2345
}

x();
</script>```

• ## Users who have thanked Philip M for this post:

charanrajd (10-10-2011)

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•