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
    Regular Coder
    Join Date
    Aug 2002
    Location
    Oregon, United States of America
    Posts
    882
    Thanks
    1
    Thanked 9 Times in 9 Posts

    Alphabetic sorting

    I have two bits of information, a name, and an ID number. I need to sort the names alphabeticaly, while stil maintaining thier accosition to the id number.

    In PHP, i would have an array, with the ID number being the key of the array, and the name being the value, and then just run sort() on it.

    How can I do this in JS?

    THank you for any help. This has been a long day of frustrating experiements in javascript for something that is so simple in other languages.
    If I'm postin here, I NEED YOUR HELP!!

  • #2
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You can use Array.sort().

    http://www.w3schools.com/jsref/jsref_sort.asp

    so if you had an array.

    Code:
    var array_of_objects = [
      { name: "Peter", id: 45 },
      { name: "John", id: 31 },
      { name: "Sally", id: 49 },
      { name: "Henry", id: 41 },
      { name: "Karen", id: 32 }
    ];
    
    function byName(a, b) {
      if (a.name == b.name) { return(0); }
      else if (a.name > b.name) { return(1); }
      else if (a.name < b.name) { return(-1); }
    }
    
    array_of_objects.sort(byName);
    Of course, this is untested but the basic idea is correct.

    david_kw
    Last edited by david_kw; 12-14-2006 at 01:42 AM.

  • #3
    New Coder
    Join Date
    Jan 2007
    Posts
    93
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I have a relatted question

    I want to sort the array by the 16 letter substring.

    I just a simple array, no collums etc

    function by16letter(a, b) {
    if (a.substring(16) == b.substring(16) { return(0); }
    ...
    ...

    would that be correct?

  • #4
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I thought substring took 2 arguments but I'm not positive. If so it would be

    function by16letter(a, b) {
    if (a.substring(0, 16) == b.substring(, 016) { return(0); }
    ...
    ...

    Which would see if the first 16 chars are the same, etc...

    david_kw

  • #5
    Regular Coder
    Join Date
    Aug 2002
    Location
    Oregon, United States of America
    Posts
    882
    Thanks
    1
    Thanked 9 Times in 9 Posts
    david_kw posted the code in the second reply a long time ago. I really didn't know what it meant, or how to really use it. I have since learned enough to figure out what david_kw meant, and I want to post it here for everyone to read if they every need this info.

    First lets say that you have a bunch of Divs with information in them that you need to sort. If this were an array where teh order of the keys didnt matter, thats simple, but in my case I needed to sort by the names, but also rearrange the ids, that way I could use the sorted ids to rearrange my objects.

    Example:
    Code:
    <div id="test">
    	<div id="45">Peter</div>
    	<div id="31">John</div>
    	<div id="12">Jimmy</div>
    	<div id="52">Tommy</div>
    	<div id="46">Tyler</div>
    	<div id="49">Sally</div>
    	<div id="41">Henry</div>
    	<div id="32">Karen</div>
    </div>
    Here you have a very simple set up. Here is how david_kw suggested to organize the information:

    Code:
    var array_of_objects = [
      { name: "Peter", id: 45 },
      { name: "John", id: 31 },
      { name: "Sally", id: 49 },
      { name: "Henry", id: 41 },
      { name: "Karen", id: 32 }
    ];
    I have found out that this is called JSON. That stands for JavaScrip Object Notation. Basicly a simple way of creating a bunch of objects (or other peices of information.)

    For those who don't understand this, here is another way to build this type of information:

    Code:
    	function addNewJSON( name, id ){
    		this.name = name;
    		this.id = id;
    	}
    
    	myJSONObject[ myJSONObject.length ] = new addNewJSON('Karen', 32);
    	myJSONObject[ myJSONObject.length ] = new addNewJSON('Tommy', 52);
    	myJSONObject[ myJSONObject.length ] = new addNewJSON('Jimmy', 12);
    	myJSONObject[ myJSONObject.length ] = new addNewJSON('Tyler', 46);
    Either way, you're going to end up with a bunch of objects. You access them like this:

    Code:
    myJSONObject[2].name
    Now, for the sorting of the objects, you use david_kw's method like so:

    Code:
    function byName(a, b) {
    	  if (a.name == b.name) { return(0); }
    	  else if (a.name > b.name) { return(1); }
    	  else if (a.name < b.name) { return(-1); }
    	}
    	
    	myJSONObject.sort(byName);
    And then to sort the divs, you do this:

    Code:
    	function sort_dom( list, obj ){
    		for(x=0; x<list.length; x++ ){
    			original = document.getElementById( list[x].id );
    			temp = original.cloneNode(true);
    			obj.removeChild( original );
    			obj.appendChild( temp );
    		}
    	}
    
    	sort_dom( myJSONObject, document.getElementById('test') );


    All in all you end up with this:

    Code:
    <div id="test">
    	<div id="45">Peter</div>
    	<div id="31">John</div>
    	<div id="12">Jimmy</div>
    	<div id="52">Tommy</div>
    	<div id="46">Tyler</div>
    	<div id="49">Sally</div>
    	<div id="41">Henry</div>
    	<div id="32">Karen</div>
    </div>
    
    <script>
    	function addNewJSON( name, id ){
    		this.name = name;
    		this.id = id;
    	}
    	
    	var myJSONObject = [
    	  { name: "Peter", id: 45 },
    	  { name: "John", id: 31 },
    	  { name: "Sally", id: 49 },
    	  { name: "Henry", id: 41 },
    	];
    	
    	myJSONObject[ myJSONObject.length ] = new addNewJSON('Karen', 32);
    	myJSONObject[ myJSONObject.length ] = new addNewJSON('Tommy', 52);
    	myJSONObject[ myJSONObject.length ] = new addNewJSON('Jimmy', 12);
    	myJSONObject[ myJSONObject.length ] = new addNewJSON('Tyler', 46);
    	
    	function sort_dom( list, obj ){
    		for(x=0; x<list.length; x++ ){
    			original = document.getElementById( list[x].id );
    			temp = original.cloneNode(true);
    			obj.removeChild( original );
    			obj.appendChild( temp );
    		}
    	}
    	
    	function byName(a, b) {
    	  if (a.name == b.name) { return(0); }
    	  else if (a.name > b.name) { return(1); }
    	  else if (a.name < b.name) { return(-1); }
    	}
    	
    	myJSONObject.sort(byName);
    	sort_dom( myJSONObject, document.getElementById('test') );
    </script>

    Now that I have figured out what all of this JSON sorting crap is, I must say that it is VERY helpful, and I send a big thank you out to david_kw. I hope this helps someone in the future.
    If I'm postin here, I NEED YOUR HELP!!


  •  

    Posting Permissions

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