By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,377 Members | 1,655 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,377 IT Pros & Developers. It's quick & easy.

Please help

P: n/a
Hi,

I am very new to Javascript, and have some problems with the code for
sorting. My question is in the following web page:

www.pinyinology.com/sort1.html

Thanks for help.

Juli Zhang

Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
du*******@yahoo.com wrote:
I am very new to Javascript, and have some problems with the code for
sorting. My question is in the following web page: [...]


Untested quickhack for IDs matching /\D+-\d+/:

function sortByID(a, b) {
var A = a["ID"].toLowerCase().replace(/\d/g,""),
B = b["ID"].toLowerCase().replace(/\d/g,""),
C = a["ID"].replace(/\D/g,""),
D = b["ID"].replace(/\D/g,"");
if (A < B) return -1;
else if (A > B) return 1;
else if (C - D < 0) return -1;
else if (C - D > 0) return 1;
else return 0;
}

In addition to that, you should test if a function exists that
is named like the value you get from the location's search before
you call it:

var sortCriteria = location.search.substring(1);
if (sortCriteria && typeof window[sortCriteria] == "function")
kidney.sort(window[sortCriteria]);

ciao, dhgm
Jul 23 '05 #2

P: n/a
Dear Dietmar Meier:

Thank you very, very much. The code works perfectly. It is now
sorting correctly:

www.pinyinology.com/sort2.html

Juli Zhang

Jul 23 '05 #3

P: n/a
du*******@yahoo.com wrote:
The code works perfectly. It is now sorting correctly: [...]


There's still an error in the other function, sortByPinyin().

Replace

var A = a["Pinyin"].toString.toLowerCase();
var B = b["Pinyin"].toString.toLowerCase();

with

var A = a["Pinyin"].toString().toLowerCase();
var B = b["Pinyin"].toString().toLowerCase();

or, since all these values are of type "string" already, simply

var A = a["Pinyin"].toLowerCase();
var B = b["Pinyin"].toLowerCase();

in lines 134 and 135.

ciao, dhgm
Jul 23 '05 #4

P: n/a

Dietmar Meier wrote:
du*******@yahoo.com wrote:
The code works perfectly. It is now sorting correctly: [...]


There's still an error in the other function, sortByPinyin().

Replace

var A = a["Pinyin"].toString.toLowerCase();
var B = b["Pinyin"].toString.toLowerCase();

with

var A = a["Pinyin"].toString().toLowerCase();
var B = b["Pinyin"].toString().toLowerCase();

or, since all these values are of type "string" already, simply

var A = a["Pinyin"].toLowerCase();
var B = b["Pinyin"].toLowerCase();

in lines 134 and 135.

ciao, dhgm


Errors corrected accordingly. Now it is in:

www.pinyinology.com/sortable/sort2.html

Thanks again for expertise and kindness.

Sincerely,
Juli Zhang

Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.