Agent Moose Posted November 22, 2006 Share Posted November 22, 2006 I can't figure out how to sort with Javascript. I am trying to make a code that when you click on a letter it sorts out all the names that start with that letter you clicked on. How can I do this? Link to comment Share on other sites More sharing options...
Chocolate570 Posted November 22, 2006 Share Posted November 22, 2006 You'd have to loop through all of the names. While in the loop, substring the first letter of the current name out. Check if it is the letter you're trying to sort. If it is, add it to an array.At the end of the loop, just display all of the array elements. Link to comment Share on other sites More sharing options...
jesh Posted November 22, 2006 Share Posted November 22, 2006 You might also be able to use the sort method of the array class. Check this link:http://www.w3schools.com/jsref/jsref_sort.asp Link to comment Share on other sites More sharing options...
Agent Moose Posted November 23, 2006 Author Share Posted November 23, 2006 1st person who posted* I am a bit confused by yours. Do you think you can just give me an example of one or two letters?2nd person who posted* I am not looking for something like that. I am look so that when you click on a letter, whatever letter you click on it will sort those ones...I guess I will have to make an exra page to do it though...EDIT: Here is a link to what i want to be sorted...it is a radio code I made...click here Link to comment Share on other sites More sharing options...
Chocolate570 Posted November 23, 2006 Share Posted November 23, 2006 Here: <script type="text/javascript">function sort(let) { x=[]; //This array contains all of the words. y=[]; //This will, at the end of the script, contain all words beginning with the variable let out of the array x. x[0]="hello" //Element num 1... x[1]="helo" //num 2... x[2]="ff" //num 3... for(i=0;i<x.length;i++) { //Looping through the array... if(x[i].substr(0,1).toLowerCase()==let) { //This checks if the letter at space 1 in the string is equal to the let... y[y.length]=x[i]; //If so, add it to the y variable... } } return y; //Send y to whoever's calling this thing...}</script> Basically, now if you do this:x=sort("a");The variable x, now an array, will contain all of the array elements that start with a. Is that what you'd like? Link to comment Share on other sites More sharing options...
Agent Moose Posted November 24, 2006 Author Share Posted November 24, 2006 Thanks, I will try this out.EDIT: I tried it out and I still don't get it. Do I leave everything like it is and just change the words or do I change more than that? Link to comment Share on other sites More sharing options...
Chocolate570 Posted November 24, 2006 Share Posted November 24, 2006 You just add more things to the array. For example, if I also wanted to add the words "lol", "hel", and "choc", I'd do this: <script type="text/javascript">function sort(let) { x=[]; //This array contains all of the words. y=[]; //This will, at the end of the script, contain all words beginning with the variable let out of the array x. x[0]="hello" //Element num 1... x[1]="helo" //num 2... x[2]="ff" //num 3... x[3]="lol"; x[4]="hel"; x[5]="choc"; for(i=0;i<x.length;i++) { //Looping through the array... if(x.substr(0,1).toLowerCase()==let) { //This checks if the letter at space 1 in the string is equal to the let... y[y.length]=x; //If so, add it to the y variable... } } return y; //Send y to whoever's calling this thing...}</script> See? :)Now if we do this underneath:alert(sort("h"));It'll alert this:hello,helo,hel Link to comment Share on other sites More sharing options...
Agent Moose Posted November 24, 2006 Author Share Posted November 24, 2006 It works but can we have table it it? For example...Here is a link for what I want to be sorted...http://z11.invisionfree.com/MooseCreations...act=SR&f=19If you look at the first one, it is called Active Radio X, How can I make it so that that whole table is sorted along with all the other stations that start with A? Link to comment Share on other sites More sharing options...
Chocolate570 Posted November 24, 2006 Share Posted November 24, 2006 So you want it in that format? So that you could do something like this:x[0]=["radio station link","radio station name","radio station description","genre"]And it'll replace the current tables with only the ones thats' stations start with the letter you choose?I can change the code so it does that. Link to comment Share on other sites More sharing options...
Agent Moose Posted November 25, 2006 Author Share Posted November 25, 2006 yes like that, can you do it for me? If you look at the page source of the link I gave it has all of the tables that are there and all the link and things...but you probably know that... Link to comment Share on other sites More sharing options...
Chocolate570 Posted November 25, 2006 Share Posted November 25, 2006 Sure. Just give me a little time, k? I'm slightly busy with, coincidentally, some other invisionfree codes. :)I'll post when I've got it. Link to comment Share on other sites More sharing options...
Agent Moose Posted November 25, 2006 Author Share Posted November 25, 2006 thanks. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now