...

View Full Version : Alphabetic sorting



Ultragames
12-13-2006, 11:18 PM
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.

david_kw
12-14-2006, 01:39 AM
You can use Array.sort().

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

so if you had an array.



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

tonyp12
01-15-2007, 05:18 AM
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?

david_kw
01-15-2007, 05:28 AM
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

Ultragames
03-28-2007, 11:40 AM
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:

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


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:


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:


myJSONObject[2].name

Now, for the sorting of the objects, you use david_kw's method like so:


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:


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:


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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum