PDA

View Full Version : Sorting an Arrayobject through javascript

charanrajd
10-08-2011, 07:20 PM
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

ironboy
10-08-2011, 09:14 PM
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:

var a = [1333, 'A12', 'b233', 'Abc' , 'zaa'];
a.sort(function(x,y){
return x > y ? 1 : -1
});
alert(a.join('\n'));

However that would return
A12 Abc b233 zaa 1333
which is not the order you seem to want...

Philip M
10-08-2011, 09:25 PM
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:-

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

mrhoo
10-08-2011, 10:46 PM
// 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'];

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

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

Philip M
10-09-2011, 08:45 AM
Solved "the long way" which works in IE6. But your required sort order is weird.

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