Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Oct 2011
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    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

  • #2
    Regular Coder
    Join Date
    Sep 2011
    Location
    Sweden
    Posts
    154
    Thanks
    1
    Thanked 22 Times in 22 Posts
    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
    });
    alert(a.join('\n'));
    However that would return
    A12 Abc b233 zaa 1333
    which is not the order you seem to want...
    Last edited by ironboy; 10-08-2011 at 08:25 PM.

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,142
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    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.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #4
    Regular Coder
    Join Date
    Mar 2006
    Posts
    728
    Thanks
    35
    Thanked 132 Times in 123 Posts
    // 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;
    		}
    	}
    Last edited by mrhoo; 10-09-2011 at 11:36 PM. Reason: added map shim

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,142
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    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>

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

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