473,378 Members | 1,426 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

comp

3
i am trying to pass a function to the sort method of array to sort in the following order: i want a,x,y,z to come before the rest and the rest follow the usual alphabetical order. i tried to the the following:

function myComp( a,b ){
var first = a
var second = b
if( first.toString() > second.toString() return -1
if( first.toString() < second.toString() return 1
return 0
}
trees = ["bg","td","ac","xa", "yt", "zu", "a", "x"]
document.write(trees.sort(myComp)
but this doesn't seem to work?
any help?
Apr 13 '07 #1
2 1092
mrhoo
428 256MB
Expand|Select|Wrap|Line Numbers
  1. function comp(a,b){
  2.     var ord={'a':1,'x':2,'y':3,'z':4};
  3.     if(ord[a]){
  4.         if(ord[b])return ord[a]-ord[b];
  5.         else return -1
  6.     }
  7.     if(ord[b]) return 1;
  8.     if(a==b) return 0;
  9.     return (a.toLowerCase()>b.toLowerCase())? 1: -1;
  10. }
trees = ["bg","td","ac","xa", "yt", "zu", "a", "x"]
trees.sort(comp)
/*
returns ['a','x','ac','bg','td','xa','yt','zu']
*/
Apr 13 '07 #2
ezafy
3
wow, that's cool :)
Apr 14 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Gary L. Burnore | last post by:
REQUEST FOR DISCUSSION (RFD) unmoderated group comp.databases.mysql This is an invitation to discuss the following proposal to create newsgroup comp.databases.mysql. Please note that YOU CANNOT...
8
by: William Drew | last post by:
REQUEST FOR DISCUSSION (RFD) unmoderated group comp.databases.mysql This is an invitation to discuss the following proposal to create newsgroup comp.databases.mysql. Please note that YOU...
0
by: Cara | last post by:
LAST CALL FOR VOTES (of 2) unmoderated group comp.databases.etl Newsgroups line: comp.databases.etl Extraction, transformation, loading issues. Votes must be received by 23:59:59 UTC, 12 Dec...
0
by: Cara Altman | last post by:
REQUEST FOR DISCUSSION (RFD) unmoderated group comp.databases.etl This is a formal Request For Discussion (RFD) for the creation of a world-wide unmoderated Usenet newsgroup...
0
by: Craig Rodrigues | last post by:
REQUEST FOR DISCUSSION (RFD) unmoderated group comp.object.corba.tao This is a formal Request For Discussion (RFD) to create comp.object.corba.tao as an unmoderated world-wide Usenet newsgroup...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.